在 python 中加载 DLL 时出错,不是有效的 win32 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19849077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:48:12  来源:igfitidea点击:

Error loading DLL in python, not a valid win32 application

pythondllctypes

提问by jeffpkamp

I am trying to load a DLL in python to call functions.

我正在尝试在 python 中加载一个 DLL 来调用函数。

import ctypes
from ctypes import *

dsusb = ctypes.WinDLL('c:\python27\dsusb.dll')

I get the following error in my stack.

我的堆栈中出现以下错误。

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

I also tried cdll with the same code.

我也尝试过使用相同代码的 cdll。

I looked up the error and windows says it's due to a path containing spaces... which I do not think is really the problem...

我查了一下错误,windows 说这是由于路径包含空格......我认为这不是真正的问题......

Am I loading this DLL wrong or is there something that might be wrong in the dll?

我加载这个 DLL 是错误的还是 dll 中可能有错误?

采纳答案by ABM

As the comments suggest, it could be an architecture problem.

正如评论所暗示的那样,这可能是一个架构问题。

If you're using a 32bit DLL with 64bit Python, or vice-versa, then you'll probably get errors.

如果您在 64 位 Python 中使用 32 位 DLL,反之亦然,那么您可能会遇到错误。

Since I've had your error before, I recommend trying to load your DLL with 32bit Python.

由于我之前遇到过您的错误,我建议尝试使用 32 位 Python 加载您的 DLL。

回答by Cassio

I had the same issue. I fixed by compiling my C code using the VS2015 x64 Native Tools Command Prompt. Now everything is 64bit.

我遇到过同样的问题。我通过使用 VS2015 x64 本机工具命令提示符编译我的 C 代码来修复。现在一切都是64位。

回答by remustata

That error can also come if you don't have all the other required dlls. Make sure you have all other .dll files that your dll depend on. As someone else pointed, Dependency Walker is a good tool.

如果您没有所有其他必需的 dll,也会出现该错误。确保您拥有 dll 所依赖的所有其他 .dll 文件。正如其他人指出的那样,Dependency Walker 是一个很好的工具。