导入dll文件时python ctypes失败

导入dll文件时python ctypes失败,python,dll,ctypes,Python,Dll,Ctypes,我是python的新手,现在在python中导入dll时遇到了一个问题。通过参考在线找到的一些提示,我尝试使用如下带有错误提示的ctypes >>> import ctypes >>> dl=ctypes.WinDll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll') Traceback (most recent call last): File "<pyshell#2>",

我是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] 
>>导入ctypes
>>>dl=ctypes.windl('C:\\Python27\\Lib\\site packages\\UdfManagerPython.dll')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
dl=ctypes.windl('C:\\Python27\\Lib\\site packages\\UdfManagerPython.dll')
AttributeError:“模块”对象没有属性“Windell”
>>>dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site packages\\UdfManagerPython.dll')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site packages\\UdfManagerPython.dll')
LoadLibrary中第431行的文件“C:\Python27\lib\ctypes\\uuuu init\uuuuu.py”
返回自我类型(名称)
文件“C:\Python27\lib\ctypes\\ uuuu init\ uuuu.py”,第353行,在\uuu init中__
self.\u handle=\u dlopen(self.\u名称,模式)
WindowsError:[错误126]

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

Windl应全部为小写

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

尝试
windle
CDLL


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

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

程序无法启动,因为您的计算机中缺少python22.dll。请尝试重新安装程序以解决此问题

因此,这个DLL似乎是用于Python2.2的,而不是您正在使用的Python2.7

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

还值得指出的是,您不能在Python2.2中使用
ctypes
,因为

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

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

编辑:我尝试安装Python2.2以查看是否可以加载此DLL。安装了Python2.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. win32上的Python 2.7.3(默认值,2012年4月10日23:31:26)[MSC v.1500 32位(英特尔)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>从ctypes导入* >>>l=CDLL(“UdfManagerPython.dll”) >>>l.initPyUdfNumber() 致命的Python错误:解释器未初始化(版本不匹配?) 此应用程序已请求运行时以异常方式终止它。 有关更多信息,请联系应用程序的支持团队。 我用谷歌搜索了这个错误消息的第一行,从大多数结果中我得到的反复出现的主题是,这个错误表示您正试图加载一个扩展模块,该模块链接到一个版本的Python和另一个版本的Python


因此,在回答您在评论中提出的问题时,不,我认为没有办法在Python 2.7中加载此DLL。

我尝试过。但还是不行。它说“TypeError:‘LibraryLoader’对象不可调用”是DLL内置版本还是调试版本?Windell不是小写,那是子模块或其他什么。是温德尔。更多信息请参见我的答案。Windl的大小写对我都不适用。Python用相同的错误提示删除我。我确信这个dll是发布版本。感谢Christian Witts和FogleBird。我们正在使用分子动力学模拟程序,输出数据是udf格式。该程序仅将Python2.2与原始IDE环境以及数字和科学模块结合在一起。该程序是用java编写的,提供两个模块来访问C++和Python的UDF数据文件。我安装了Python2.7,以便从udf文件中提取数据,以便通过流行的模块(如numpy、vpython)进行后处理。dll似乎是在python 2.2标准下编写的,在python>=2.2中不受支持。从您的观点来看,在Python2.7中使用此dll是否有解决方法?非常感谢,您给了我最有价值和最详细的答案!我在C:\Python27\目录中使用python 2.7的默认安装设置。我将dll复制到\Lib\site packages\中,并尝试用ctypes.cdll('FILE')、ctypes.windl('FILE')加载它,但它不起作用。我在@LukeWoodward回复下的评论中详细描述了整个故事。感谢您的提示。请参阅以下链接: