从DLL调用python函数

从DLL调用python函数,python,dll,Python,Dll,我有一个python文件,我想从中调用dll中的函数 该功能的原型是: typedef double real_T; extern real_T __declspec(dllexport) RectIntersect(const real_T rect1[8], const real_T rect2[8]); python代码: import math; import ctypes; from ctypes import * import numpy; # this module shal

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

该功能的原型是:

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

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)
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio\u py\u util.py”,第70行,在exec\u文件中
exec(代码对象、全局变量)
文件“D:\Sandboxes\SRR2T0\06_Algorithm\05_Testing\05_Test_Environment\algo\smr200_bbt\valf_tests\adma\Test.py”,第18行,在
hllDll.RectIntersect(rect1,rect2);
ValueError:调用过程的参数可能太多(超过8字节)
请帮助:(…。

C声明

real_T RectIntersect(const real_T rect1[8], ...)
完全等同于(并由C编译器替换为):

这意味着DLL中的函数实际上不需要一个包含8个double的数组,而是一个指向double的指针,该指针后面可能会有7个double来组成一个包含8个double的数组(但这部分未选中)。这意味着您需要其他方法来使用ctypes编写该数组;例如:

hllDll.RectIntersect.argtypes =[ctypes.POINTER(ctypes.c_double * 8),...]
并使用
ctypes.byref(rect1)
传递它

或者:CFFI()而不是ctypes可能没有这个问题,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)

-无法删除此评论。我尝试重新创建发布的用例。另外两个详细信息需要修复:
hllDll.RectIntersect.restype=ctypes.c\u double
,以及使用
ctypes.byref()
而不仅仅是
byref()
。之后,它对我有效。
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)