导入dll fi时python ctypes失败

2024-04-30 02:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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供您分析。非常感谢您的帮助!


Tags: ininitlibpackageslinesitectypesfile
3条回答

尝试WinDLLCDLL

我从未在ctypes中直接使用LoadLibrary,但看起来它可能仍然找不到DLL。确保它在系统路径上。(或与Python模块位于同一目录中)

温德尔应该都是小写的。

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

我将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一起使用的,而不是您正在使用的python2.7。

我没有安装Python2.2。如果你这样做了,也许你会收到不同的错误信息给我。

还值得指出的是,Python 2.2不能使用ctypes,因为^{} is only supported for Python 2.3 onwards

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

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

编辑:我试图安装Python2.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 win32
Type "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.

我在google上搜索了这个错误消息的第一行,我从大多数结果中得到的反复出现的主题是,这个错误表示您试图加载一个扩展模块,该模块链接到一个版本的Python和另一个版本的Python。

所以在回答您在评论中的问题时,不,我不认为有办法在Python2.7中加载这个DLL。

相关问题 更多 >