【发布时间】:2016-01-28 17:38:24
【问题描述】:
我试图从我的 Python 代码中调用一个来自 dll 的简单函数,使用 QtCreator 构建,但问题是参数大小总是无效的。
这是我的 test.h(test.cpp 为空):
#ifndef TEST_H
#define TEST_H
#include "test_global.h"
extern "C"
{
int TESTSHARED_EXPORT si () { return sizeof(int);}
int TESTSHARED_EXPORT add4 ( int i) { return i + 4;}
}
#endif // TEST_H
它构建为 test.dll,我可以加载它:
tdll = windll.LoadLibrary(r'X:\TMP\SRC\build-test-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\test.dll')
调用print tdll.si() 会按预期打印 4,
但是对tdll.add4(1) 的所有调用都失败并出现错误:
Traceback (most recent call last):
File "test.py", line 29, in <module>
print tdll.add4(2)
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
如果我将add4 参数包装成c_int, c_uint, c_int32 或ctypes 提供的任何其他*int 类型,也会发生同样的事情。
请帮我解决这个问题。 谢谢!
【问题讨论】: