vector<double> using ctypes for DLL in Python

Asked

Viewed 100 times

0

I own a .dll that within it has a function BOLHA that returns a double.

The problem is that BOLHA has a vector<double> in the argument.

extern "C" minhaDLL_API double BOLHA(vector<double> OI);

In fact she possesses a extern "C" which makes me believe that at the time of compiling the .dll This became a pointer to double. I tried to load this function from .dll in Python as follows:

mydll = cdll.LoadLibrary("_DLL.dll")

func = mydll.BOLHA

func.argtypes = [POINTER(c_double)]
func.restype = c_double

returnarray = (c_double * 2)(0.047948, 0.005994)

func(returnarray)   

But it returns me the error:

[Error -529697949] Windows Error 0xE06D7363

  • You cannot have C++ names exported as C names, as far as I know. And std::vector<double> is not a pointer, definitely. This code smells like undefined behavior.

  • It works correctly and compiles well as well. Only the way to access the arguments does not seem to be so simple

1 answer

1


It seems that there is no direct way to solve this using Ctypes, I found that the best way to solve this problem is by changing the . dll

It is necessary to put a double pointer on the argument and its size as integer and then at the beginning of the function you can turn this pointer + size into a vector and proceed normal with the function. This way, one can easily establish the connection with a type C without losing the utility of the vector in the code.

looks like this on . dll

extern "C" minhaDLL_API double BOLHA(double* OI, int size);

and within the function one can make the following association:

vector<double> teste(OI, OI + size);

now the test vector can be used normally.

In python, it is:

mydll = cdll.LoadLibrary("_DLL.dll")

func = mydll.BOLHA

func.argtypes = [POINTER(c_double), c_int]
func.restype = c_double

returnarray = (c_double * 2)(0.047948, 0.005994)

func(returnarray, 2)   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.