我正在尝试将C主程序写入dll中,Python将从该dll中导入所有内容(包括所有变量和函数),并运行dll中定义的函数。但是,我不仅打算将函数导出,还打算将变量从 DLL 导出到 Python。 我了解如何使用 DLL 将函数公开给 Python,但我不确定如何在 Python 中使用 Ctype 访问 dll 中的变量。
举个例子: 如果在 header 内,我们有#DEFINE MAXDEVNUMBER 4。 当我使用 ctype print mydll.MAXDENUMBER 时,它给了我一个错误。找不到函数“MAXDENUM”
最佳答案
您无法访问预处理器宏,因为它们不是从 DLL 导出的。您只能访问导出的 C 函数和全局变量。
例如,test.c:
__declspec(dllexport) int b = 5;
__declspec(dllexport) int func(int a)
{
return a + b;
}
>>> from ctypes import * >>> dll = CDLL('test') >>> dll.func(1) 6 >>> x=c_int.in_dll(dll,'b') # access the global variable >>> x.value 5 >>> x.value = 6 # change it >>> dll.func(1) 7
关于python - C.dll 如何向 Python 公开变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44192157/