Using ctypes in python to access C # DLL tools

I would like to implement C # code in the critical part of my python program to make it faster. It says (according to Python documentation and this site ) that you can download a dynamic link library (and PyDocs, so to speak) as follows:

cdll.LoadLibrary("your-dll-goes-here.dll")

This is the part of my code that performs this function:

 from ctypes import * z = [0.0,0.0] c = [LEFT+x*(RIGHT-LEFT)/self.size, UP+y*(DOWN-UP)/self.size] M = 2.0 iterator = cdll.LoadLibrary("RECERCATOOLS.dll") array_result = iterator.Program.ITERATE(z[0],z[1],c[0],c[1],self.iterations,M) z = complex(array_result[0],array_result[1]) c = complex(array_result[2],array_result[3]) last_iteration = int(round(array_result[4])) 

And the RECERCATOOLS.dll I'm using is this (C # code, not C or C ++):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using KarlsTools; public class Program { public static Array ITERATE(double z_r,double z_i,double c_r, double c_i, int iterations, double limit) { Complex z = new Complex(z_r, z_i); Complex c = new Complex(c_r, c_i); for (double i = 1; Math.Round(i) <= iterations; i++) { z = Complex.Pow(z, 2) + c; if (Complex.Abs(z) < limit) { double[] numbers = new double[] { Complex.Real(z), Complex.Imag(z), Complex.Real(c), Complex.Imag(c), i}; return numbers; } } double iter = iterations; double[] result = new double[] { Complex.Real(z), Complex.Imag(z), Complex.Real(c), Complex.Imag(c), iter}; return result; } } 

To create this DLL, I use the Build command on the Visual Studio 2010 project, which contains only this file and a link to Karlstools, a module that allows me to use complex numbers.

I don't know why, but when I try to run my Python code, it just throws an exception:

  [...] array_result = iterator.Program.ITERATE(z[0],z[1],c[0],c[1],self.iterations,M) File "C:\Python32\lib\ctypes\__init__.py", line 353, in __getattr__ func = self.__getitem__(name) File "C:\Python32\lib\ctypes\__init__.py", line 358, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'Program' not found 

I need help with this, as it continues to throw me exceptions, even if everything is set to public and the function is static , or even if I try to access the function directly without specifying the "Program" class ... I donโ€™t know where problem.

+8
python c # dll load ctypes
source share
5 answers

The tips you'll find about calling a DLL from Python using ctypes rely most of the time on a DLL written in C or C ++, not in C #. With C # you pull the whole CLR mechanism, and the characters are most likely crippled, not what ctypes are expecting, and you will get all kinds of problems from the garbage collection of your output array.

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

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

+7
source share

This 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 more details.

Then you can directly export without executing the COM level. Here is a sample C # code:

 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; } } 

Then you can load the DLL and call the public methods in Python (works for version 2.7)

 import ctypes a = ctypes.cdll.LoadLibrary(source) a.add(3, 5) 
+10
source share

Others have indicated that C # DLLs cannot be handled in the same way as a native DLL.

One option is to export your C # functionality as a COM object that Python can easily use.

Personally, I would consider a solution based on my own code, but you may well be too committed to C # to change course at this point.

+3
source share

ctypes designed to load shared libraries written in C (or at least shared libraries that are directly executed by the processor and export their characters and function arguments following standard C conventions.) C # DLLs are not โ€œnormalโ€ DLLs, but rather Designed to work in the .NET environment.

Your options:

  • Use some kind of Python modem for .NET.
  • Write the DLL in C and use ctypes to call the function and process the data.
  • Use the Python / C API and create a Python loadable module.

I can not talk with option number 1, I did not. Option # 2 is usually simpler than option # 3, in which you write clean C code and stick it with ctypes . Option number 3 will offer the best performance for all three options, since it has the lowest maintenance costs and runs its own processor on the processor.

+1
source share

C # DLL is really called an assembly, so it is completely different. If there is a very important reason, I suggest you use C ++

+1
source share

All Articles