返回結構我有一個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