【发布时间】:2013-11-07 23:49:03
【问题描述】:
我正在尝试在 python 中加载一个 DLL 来调用函数。
import ctypes
from ctypes import *
dsusb = ctypes.WinDLL('c:\python27\dsusb.dll')
我的堆栈中出现以下错误。
C:\Python27>python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
dsusb = ctypes.WinDLL('c:\python27\dsusb.dll')
File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application
我也用相同的代码尝试了 cdll。
我查看了错误,windows 说这是由于路径包含空格...我认为这不是真正的问题...
是我加载此 DLL 错误还是 dll 中可能有问题?
【问题讨论】:
-
确保架构匹配——32 位 Python 到 32 位 DLL,等等。
WinDLL切换到 stdcall 调用约定。从标题中应该清楚使用哪种约定。 -
使用原始字符串作为路径文字;
'p'和'd'没问题,但一般来说你会省去头痛。或者只使用正斜杠。 Windows 文件 API 将它们转换为反斜杠,但使用'\\?\'前缀的长 Unicode 路径除外。 -
我将 \ 更改为 /,但输出没有变化。我不确定,但我相信我正在运行 32 位的 python(不知道在哪里检查 python 的版本)。不知道如何读取 DLL 头......还有其他想法吗?
-
platform.architecture()返回 Python 进程是 32 位还是 64 位,但这只是基于指针大小,您可以使用ctypes.sizeof(ctypes.c_void_p)来完成。对于 DLL,您可以使用 Dependency Walker 或 Visual Studio 的 dumpbin。 -
看起来是因为我用 64 位 python 打开了 32 位 dll,你应该给出答案,这样我就可以投票了。