passing vector from a c++ dll in python ctypes

0

I have a .dll that was written in c++ with extern "C", this function is called BOLHA and returns a double.

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

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

in fact due the extern "C" I believe that this vector turns into a pointer and so I tried to load the function 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 I get the following error.

[Error -529697949] Windows Error 0xE06D7363
python
c++
dll
ctypes
asked on Stack Overflow Apr 10, 2018 by Patrick Machado

1 Answer

1

in fact due the extern "C" I believe that this vector turns into a pointer

This is wrong. there is no mechanism that makes this possible by default. Your BOLHA() function needs to receive a double* and then convert it to vector<double> or just use the raw pointer.

If you want this to work with the vector<double> signature, you needs something to do the work of converting the pointer to a vector. boost::python can do that but that would require that the DLL you're working with would be a python module and not just any DLL.

if you have the function:

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

you'll need to declare a new function:

extern "C" myDLL_API double BOLHA_raw(double* ptr, int size) {
    vector<double> v(ptr, ptr+size);
    return BOLHA(v);
}
answered on Stack Overflow Apr 10, 2018 by shoosh • edited Apr 10, 2018 by shoosh

User contributions licensed under CC BY-SA 3.0