Using ctypes in python to acces a C# dll's methods -
Using ctypes in python to acces a C# dll's methods -
i implement c# code in critical part of python programme create faster. says (on python documentation , this site) can load dynamic link library (and pydocs) follows:
cdll.loadlibrary("your-dll-goes-here.dll")
this part of code takes care of feature:
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 recercatools.dll utilize (c# code, not c or c++):
using system; using system.collections.generic; using system.linq; using system.text; using karlstools; public class programme { 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); (double = 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}; homecoming numbers; } } double iter = iterations; double[] result = new double[] { complex.real(z), complex.imag(z), complex.real(c), complex.imag(c), iter}; homecoming result; } } to build dll utilize "build" command on visual studio 2010 project, contains file , reference "karlstools", module allows me utilize complex numbers.
i don't know why when seek run python code, throws 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 this, since keeps throwing me exceptions set public , function static, or when if seek access function straight without specifying "program" class... have no clue problem be.
the tips you'll find regarding calling dll python using ctypes rely of time dll beingness written in c or c++, not c#. c# pull in whole machinery of clr, , symbols mangled , not ctypes expect, , you'll sorts of troubles garbage collection of output array.
i've had success when interfacing python , c# code using python dot net (http://pythonnet.sf.net), may want seek this.
on other hand if in pure performance, consider rewriting code native c extension python using python/c api (http://docs.python.org/c-api/).
c# python dll load ctypes
Comments
Post a Comment