返回结构我有一个DLL写在C出口这样的功能:错误从DLL函数在Python
typedef struct testResult_t {
int testId;
int TT;
double fB;
double mD;
double mDL;
int nS;
int nL;
} TestResult;
TestResult __stdcall dummyTest(){
TestResult a = {0};
a.testId = 3;
return a;
};
我用Python调用函数是这样的:
class TestResult(Structure):
_fields_ = [
("testId", c_int),
("TT", c_int),
("fB", c_double),
("mD", c_double),
("mDL", c_double),
("nS", c_int),
("nL", c_int)
]
astdll.dummyTest.restype = TestResult
result = astdll.dummyTest()
print "Test ID: %d" % (result.testId)
我执行脚本时出现此错误:
Traceback (most recent call last):
File "ast.py", line 330, in <module>
main()
File "ast.py", line 174, in main
result = astdll.dummyTest()
File "_ctypes/callproc.c", line 941, in GetResult
TypeError: an integer is required
任何想法有什么问题?
你应该显示一切。你已经省略了重要的细节。我们看不到'astdll'是什么。更大的问题是不同的C编译器对于返回大型结构体有不同的ABI。值得注意的是MSVC和GCC对你的功能有不同的ABI。使用引用参数ctypes.byref返回结构是设计此接口的最佳方法。 – 2013-05-08 02:47:30