使用ctypes模块访问用C编写的DLL时出错

我有一个DLL,只有一个函数,它有五个双精度数和一个整数:

__declspec(dllexport) struct res ITERATE(double z_r,double z_i,double c_r, double c_i, int iterations, double limit) 

它返回一个自定义的struct caled res,它由一个三双数组组成:

 struct res { double arr[3]; }; 

要返回值,我这样做:

 struct res result; /*earlier in the code */ result.arr[0] = z_real; /*Just three random doubles*/ result.arr[1] = z_imag; result.arr[2] = value; return result; 

我用MinGW编译它,我试图在python中使用它来做这样的事情:

 form ctypes import * z = [0.0,0.0] c = [1.0,1.0] M = 2.0 MiDLL = WinDLL("RECERCATOOLS.dll") MiDLL.ITERATE.argtypes = [c_double, c_double, c_double, c_double,c_int,c_double] MiDLL.ITERATE(z[0],z[1],c[0],c[1],100,M) #testing out before assigning the result to anything. 

但是,每当我尝试使用这些值调用函数时,它都会抛给我:

 WindowsError: exception: access violation writing 0x00000000 

我也不知道如何捕获我声明的自定义结构并将其中的每个元素转换为Python浮点数。 我已经研究过这个PyDocs链接,但无济于事。

先感谢您。

编辑:

这是原始(根据建议修改)标题使用(“mydll.h”):

 #ifndef MYDLL_H #define MYDLL_H extern "C" __declspec(dllexport) #define EXPORT_DLL __declspec(dllexport) EXPORT_DLL void ITERATE(struct res*, double z_r,double z_i,double c_r, double c_i, int iterations, double limit) #endif 

并且,万一它可能有问题,代码文件(它很短,只有一个function):

 #include  #include  struct res { double arr[3]; }; void __declspec(dllexport) ITERATE(struct res* result,double z_r,double z_i,double c_r, double c_i, int iterations, double limit) { /* The purpose of this function is, given two complex numbers, an iteration number and a limit, apply a formula to these two numbers for as many iterations as specified. If at any iteration the result of the formula is bigger than the limit, stop and return the number and the iteration it reached. If after iterating they are still inside the limit, return the number after all the iterations and the number of iterations it has gone through. Complex numbers are composed of a real part and an imaginary part, and they must be returned separately. */ double complex z = z_r + z_i*I; double complex c = c_r + c_i*I; int actual_iter; for (actual_iter = 1; actual_iter  limit) { double value = actual_iter; double z_real = creal(z); double z_imag = cimag(z); result.arr[0] = z_real; result.arr[1] = z_imag; result.arr[2] = value; } } double value = iterations; double z_real = creal(z); double z_imag = cimag(z); result.arr[0] = z_real; result.arr[1] = z_imag; result.arr[2] = value; } int main() { return 0; } 

返回像这样的结构有一个问题。 并非所有编译器都以相同的方式返回此类结构。 我宁愿将函数声明更改为:

 void __declspec(dllexport) ITERATE(struct res* result, double z_r,double z_i, double c_r, double c_i, int iterations, double limit); 

这样结构就在用户的内存中,并且没有关于如何返回结构的歧义。

当然,正如大卫所说,你可能不得不使用不同的调用约定。