一尘不染

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

python

我有一个带有一个单一函数的DLL,它具有五个双精度和一个整数:

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

它重新调整了由三重数组组成的自定义结构计算的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

并且,如果代码可能有问题,那么代码文件(它很短,只有一个函数):

#include <stdio.h>
#include <complex.h>

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;
}

阅读 205

收藏
2021-01-20

共1个答案

一尘不染

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

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

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

当然,正如David所说,您可能必须使用其他调用约定。

2021-01-20