Using ctypes in python to access a C# dll's methods

前端 未结 5 2192
生来不讨喜
生来不讨喜 2020-12-16 02:48

I would like to implement C# code in a critical part of my python program to make it faster. It says (on Python documentation and this site) that you can load a Dynamic Link

5条回答
  •  无人及你
    2020-12-16 03:09

    The tips you'll find regarding calling DLL from Python using ctypes rely most of the time to the DLL being written in C or C++, not C#. With C# you pull in the whole machinery of the CLR, and the symbols are most likely mangled and not what ctypes expect, and you'll get all sorts of troubles from the garbage collection of your output array.

    I've had very good success when interfacing python and C# code by using python for dot net (http://pythonnet.sf.net), you may want to try this.

    On the other hand if you are in for pure performance, consider rewriting your code as a native C extension for Python using the Python/C API (http://docs.python.org/c-api/).

提交回复
热议问题