gpt4 book ai didi

导入dll文件时python ctypes失败

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:14 24 4
gpt4 key购买 nike

我是 python 的新手,现在在 python 中导入 dll 时遇到问题。通过引用网上找到的一些提示,我尝试使用 ctypes 如下错误提示。

>>> import ctypes
>>> dl=ctypes.WinDll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
dl=ctypes.WinDll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')
AttributeError: 'module' object has no attribute 'WinDll'

>>> dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')
File "C:\Python27\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "C:\Python27\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126]

是我做错了什么,还是这个 dll 没有编写符合 python 标准的脚本?我已经上传到MediaFire供您分析。非常感谢任何帮助!

最佳答案

我将 DLL UdfManagerPython.dll 加载到 Dependency Walker 中,并指出此 DLL 依赖于 python22.dll。当我试图在 Python (2.7) 解释器中加载这个 DLL 时,我得到一个消息框,它或多或少告诉我同样的事情:

The program can't start because python22.dll is missing from your computer. Try reinstalling the program to fix this problem.

所以看起来这个 DLL 旨在与 Python 2.2 一起使用,而不是您正在使用的 Python 2.7。

我没有安装 Python 2.2。如果这样做,您可能会收到不同的错误消息。

同样值得指出的是,您不能在 Python 2.2 中使用 ctypes,因为 ctypes is only supported for Python 2.3 onwards .

我不知道这个 DLL 是从哪里来的。我用谷歌搜索了它的名字,得到了所有四个结果,其中之一就是这个问题。

顺便说一句,如果可以找到 DLL 本身但缺少 DLL 的依赖项,我会看到“找不到指定的模块”形式的错误。因此,如果您收到这样的消息,并且您确定 DLL 本身存在,请检查其依赖项。

编辑:我尝试安装 Python 2.2 以查看是否可以加载此 DLL。安装 Python 2.2 后,您至少可以加载此 DLL,但如果您尝试调用任一 init... 方法,Python 就会崩溃。 (我不知道给他们传什么参数,所以我没有给他们传。)

以下是我尝试调用其中一种方法时发生的情况:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> from ctypes import *>>> l = CDLL("UdfManagerPython.dll")>>> l.initPyUdfNumber()Fatal Python error: Interpreter not initialized (version mismatch?)This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.

我用谷歌搜索了这条错误消息的第一行,我从大部分结果中得到的反复出现的主题是这个错误表明你正在尝试加载一个扩展模块,该扩展模块链接到一个版本的 Python 和一个不同版本的 Python .

所以在回答你在评论中的问题时,不,我不相信有一种方法可以在 Python 2.7 中加载这个 DLL。

关于导入dll文件时python ctypes失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com