Home >Backend Development >C++ >How Can I Call C# Libraries from Python Without Using IronPython?
Calling C# Libraries from Python
Attempting to invoke C# libraries from Python has been a common issue. While options like IronPython may encounter compatibility challenges, an alternative approach is to call C# code from Python.
To achieve this, follow these steps:
Utilizing the UnmanagedExports NuGet Package
Integrate the "UnmanagedExports" NuGet package into your .Net project, as outlined at https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports.
Exporting Functions without COM
With "UnmanagedExports," you can export functions directly, bypassing the COM layer. Consider this sample C# code:
using System; using RGiesecke.DllExport; class Test { [DllExport("add", CallingConvention = CallingConvention.Cdecl)] public static int TestExport(int left, int right) { return left + right; } }
Loading and Invoking Exported Functions in Python (2.7)
In Python, you can load the DLL and call the exported methods as follows:
import ctypes a = ctypes.cdll.LoadLibrary(source) a.add(3, 5)
The above is the detailed content of How Can I Call C# Libraries from Python Without Using IronPython?. For more information, please follow other related articles on the PHP Chinese website!