el *_*roo 2 c c++ python dll ctypes
我正在尝试将 C 主程序写入 dll,Python 将从该 dll 导入所有内容(包括所有变量和函数),并运行 dll 中定义的函数。但是,我不仅打算将函数导出,还打算将变量从 DLL 导出到 Python。我了解如何使用 DLL 将函数公开给 Python,但我不确定如何在 Python 中使用 Ctype 访问 dll 中的变量。
让我们举个例子:如果在标头内,我们有 #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;
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)>>> 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