Answers for "implement python with c#"

1

call python script from c#

private void run_cmd(string cmd, string args)
{
     ProcessStartInfo start = new ProcessStartInfo();
     start.FileName = "my/full/path/to/python.exe";
     start.Arguments = string.Format("{0} {1}", cmd, args);
     start.UseShellExecute = false;
     start.RedirectStandardOutput = true;
     using(Process process = Process.Start(start))
     {
         using(StreamReader reader = process.StandardOutput)
         {
             string result = reader.ReadToEnd();
             Console.Write(result);
         }
     }
}
Posted by: Guest on September-01-2021
0

call c# from python

"""
It is actually pretty easy. Just use NuGet to add the "UnmanagedExports" package 
to your .Net project. 
See https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports for details.
"""
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}


"""
You can then load the dll and call the exposed methods in Python (works for 2.7)
"""
import ctypes
a = ctypes.cdll.LoadLibrary(source)
a.add(3, 5)
Posted by: Guest on October-05-2021

Python Answers by Framework

Browse Popular Code Answers by Language