我有一个单一的功能换成五个双打和一个INT DLL中的DLL:错误使用ctypes的,当模块接取C语言编写的
__declspec(dllexport) struct res ITERATE(double z_r,double z_i,double c_r, double c_i, int iterations, double limit)
它retuns自定义结构caled RES它由三个-double阵列:
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;
我和敏编译它GW,我想在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浮点。我已经看过this PyDocs link,但无济于事。
预先感谢您。
编辑:
这是所使用的原始(根据建议修改)标题( “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
而且,在一些情况下可能是错的,代码文件(它很短,只有一个函数):
#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 <= iterations; actual_iter++)
{
z = z*z + c;
if (cabs(z) > 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;
}
+0
现在你已经做了这个改变,你还需要修改你的python程序来传递指向该结构的指针作为第一个参数。看看你发布的链接,你如何做到这一点。如果这不能发布与修改的C代码相匹配的Python代码。 –
+0
是的,我可以通过查看PyDocs和SO的人员来完成这项工作,但我终于完成了它的工作。 :d –
+0
你可以使用我的回答: http://stackoverflow.com/questions/7586504/python-accessing-dll-using-ctypes/13167362#13167362 –