0

我有一个 python 文件,我想从中调用一个 dll 中的函数。

该函数的原型是:

typedef double real_T;
extern real_T __declspec(dllexport) RectIntersect(const real_T rect1[8], const real_T rect2[8]);

蟒蛇代码:

import math;
import ctypes;
from ctypes import *
import numpy;


# this module shall always be executed directly
if __name__ == '__main__':
    print "Program started !";
    rect = ctypes.c_double * 8;
    rect1 = rect(1.1, 2.45, 3, 4, 5, 6, 7, 8);
    rect2 = rect(1.6, 3.45, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1);

    # Load DLL into memory.

    hllDll = ctypes.WinDLL ("IntersectDLL.dll");
    hllDll.RectIntersect.argtypes =[ctypes.c_double * 8, ctypes.c_double * 8];
    hllDll.RectIntersect (rect1, rect2);

我得到错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 70, in exec_file
    exec(code_obj, global_variables)
  File "D:\Sandboxes\SRR2T0\06_Algorithm\05_Testing\05_Test_Environment\algo\smr200_bbt\valf_tests\adma\test.py", line 18, in <module>
    hllDll.RectIntersect (rect1, rect2);
ValueError: Procedure probably called with too many arguments (8 bytes in excess)

请帮忙 :(。 ....

4

1 回答 1

1

C 声明

real_T RectIntersect(const real_T rect1[8], ...)

完全等同于(并被 C 编译器替换为):

real_T RectIntersect(const real_T *rect1, ...)

这意味着 DLL 中的函数实际上并不需要一个包含 8 个双精度数的数组,而是一个指向双精度数的指针,它后面可能会再跟着 7 个以组成一个由 8 个双精度数组成的数组(但这部分没有被检查)。这意味着您需要其他方式来使用 ctypes 编写它;例如:

hllDll.RectIntersect.argtypes =[ctypes.POINTER(ctypes.c_double * 8),...]

并通过它ctypes.byref(rect1)

或者:CFFI( http://cffi.readthedocs.org/ )可能没有这个问题,而不是 ctypes,它更接近 C 类型:

import cffi
ffi = cffi.FFI()
ffi.cdef("""
    typedef double real_T;
    real_T RectIntersect(const real_T rect1[8], const real_T rect2[8]);
""")
lib = ffi.dlopen("IntersectDLL.dll")
rect1 = ffi.new("real_T[8]", [1.1, 2.45, 3, 4, 5, 6, 7, 8])
rect2 = ffi.new("real_T[8]", [1.6, 3.45, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1])
lib.RectIntersect(rect1, rect2)
于 2013-08-23T13:23:32.953 回答