简体   繁体   English

从python ctypes中的c ++ dll传递矢量

[英]passing vector from a c++ dll in python ctypes

I have a .dll that was written in c++ with extern "C" , this function is called BOLHA and returns a double. 我有一个用c ++用extern "C"编写的.dll,此函数称为BOLHA,并返回一个double。

the problem is that BOLHA has a vector<double> in the argument. 问题在于BOLHA在参数中有一个vector<double>

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. 实际上由于extern "C"我相信此向量变成了指针,因此我尝试按以下方式加载该函数。

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

in fact due the extern "C" I believe that this vector turns into a pointer 实际上由于extern“ C”我相信这个向量变成了一个指针

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. 您的BOLHA()函数需要接收一个double* ,然后将其转换为vector<double>或仅使用原始指针。

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. 如果希望它与vector<double>签名一起使用,则需要完成将指针转换为矢量的工作。 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. boost::python可以做到这一点,但这需要您使用的DLL是python模块,而不仅仅是任何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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM