【发布时间】:2013-06-06 19:23:30
【问题描述】:
我一直在尝试在 Ctypes 中传递一个结构。但是函数调用抛出了格式错误。
这是我的 C 函数:
typedef struct Point2Struct {
double x, y;
} Point2;
Point2 test(Point2 k)
{
return k;
}
python调用如下:
class Point2(Structure):
_fields_ = [('x',c_double),('y',c_double)]
lib.test.argtypes=[Point2]
lib.test.restype=Point2
p = Point2(1.1, 2.2)
g = lib.test(p)
print g.x, g.y
当我通过 CDLL 调用函数时,我得到:
ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong calling convention
使用 WinDLL,我得到:
ValueError: Procedure probably called with too many arguments (16 bytes in excess)
我在 Windows 7 下使用 (Mingw) gcc 将 C 代码编译成 DLL。
gcc -shared -o test.dll test.o
我也尝试使用 .so 文件:
gcc -shared -Wl,-soname,test-o test.so -fPIC test.c
但是我得到了同样的错误。
我做错了什么?我应该使用任何特定选项进行编译吗?
【问题讨论】:
-
您是否尝试过将
__declspec(dllexport)和__cdecl显式添加到函数中?或者,您是否尝试过编写 C 代码,以与ctypes相同的方式加载和使用您的 DLL?