Home >Backend Development >C++ >How Can I Call a C# Library (Including WPF) from Python?
Invoking a C# Library from Python
In response to the query regarding calling a C# library (particularly WPF) from Python code, there is a straightforward solution utilizing NuGet and the "UnmanagedExports" package.
Implementation:
[DllExport("add", CallingConvention = CallingConvention.Cdecl)] public static int TestExport(int left, int right) { return left + right; }
Integration with Python:
Load the exported DLL into Python using ctypes. For Python 2.7, the following code demonstrates how to import and call the "add" function:
import ctypes a = ctypes.cdll.LoadLibrary(source) a.add(3, 5)
By utilizing this approach, you can seamlessly call C# library functions from Python code, bridging the compatibility gap between the two languages.
The above is the detailed content of How Can I Call a C# Library (Including WPF) from Python?. For more information, please follow other related articles on the PHP Chinese website!