这可能是因为编译器修改了函数名。有两种方法可以解决这个问题:
- 修复代码以告诉编译器不要破坏名称。 (查找添加外部引用)
- 找到损坏的名称并从 python 调用它(参见下面的描述)
阅读以下内容(来自http://docs.python.org/2/library/ctypes.html)
有时,dll 导出名称不是有效 Python 标识符的函数,例如“??2@YAPAXI@Z”。在这种情况下,您必须使用 getattr() 来检索函数:
>>>
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")
<_FuncPtr object at 0x...>
>>>
在 Windows 上,某些 dll 不是按名称而是按序号导出函数。可以通过使用序号索引 dll 对象来访问这些函数:
>>>
>>> cdll.kernel32[1]
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "ctypes.py", line 310, in __getitem__
func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
如果您不确定函数名称可能是什么,请查找 link.exe dumpbin.exe。这些可以在 Visual Studio 安装中找到,它们会转储 dll 中可用的所有功能。您可以对结果运行 grep。