本文介绍了C DLL加载在C ++程序中,而不是在python Ctypes中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于python / ctypes和dll加载的问题。



我已经使用VSExpress 2010创建了一个C Dll(extern C声明)。我可以使用CPP测试程序加载并执行此dll。但是,当我尝试通过ctypes加载python中的dll时,我得到旧的WindowsError:[Errno 126]找不到指定的模块错误消息。



到目前为止,我已经完成了以下操作:



1。)三重检查DLL上的路径python一边尝试一个绝对的路径来完全确定该DLL正在被发现。



2。)在依赖关系树中(在Win 7上,似乎没有错过),在但是从研究看来,似乎没有什么地方,任何人都知道一个依赖类型的工具,更多的Win7兼容?)



3。)我有一个.bat文件设置我所有的环境变量,并采取其他行动(共同定位依赖关系),以确保所有依赖关系得到解决。再次,除了典型的Win7操作系统故障之外,依赖的所有内容都显示清晰。任何人都知道一个很好的方式来调试这种类型的问题吗?



更好的版本取决于?查看python中什么符号/模块无法加载的方法?



CPP测试程序再次加载并执行完整的DLL。真的不知道Python方面发生了什么。谢谢。

解决方案

使用。配置它来监视 python.exe 进程 CreateFile 操作:





然后运行你的脚本在下面的例子中,我跑了:

 >>>来自ctypes import * 
>>> CDLL('x')
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件D:\dev\Python33x64\lib\ctypes\__init __。py,第353行,__init__
self._handle = _dlopen(self._name,mode)
OSError:[WinError 126]找不到指定的模块

x.dll 不存在,跟踪结果靠近底部,但请注意,任何丢失的依赖DLL都会显示出来:




Question regarding python/ctypes and dll loading.

I have created a C Dll (extern C declarations) using VSExpress 2010. I can load and execute this dll with a CPP test program. However, when I attempt to load the dll in python via ctypes, I get the old "WindowsError: [Errno 126] The specified module could not be found" error message.

So far I have done the following:

1.) Triple checked the path to the DLL on the python side and tried an absolute path to be completely sure the DLL is being found.

2.) Brought up the DLL in depends.exe and verified nothing seems to be missing in the dependency tree (on Win 7 so the depends output is a little flaky, but from research nothing seems out of place, anyone know of a depends-like tool that is more Win7 compatible?)

3.) I have a .bat file that sets all my environment variables and have taken other actions as well (co-locating dependencies) to make sure all dependencies are resolved. Again, everything in depends is showing clean except the typical Win7 OS failures.

Anyone know a good way to debug this type of problem? A better version of depends? A way to see exactly what symbol/module failed to load from python?

Again, the CPP test program loads and execute the dll perfectly. Really not sure what is happening on the Python side. Any help in debugging is appreciated.

Thanks.

解决方案

Use Sysinternals Process Monitor. Configure it to monitor the python.exe process for CreateFile operations:

Then run your script. In the example below, I ran:

>>> from ctypes import *
>>> CDLL('x')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\dev\Python33x64\lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

x.dll does not exist, with the following result near the bottom of the trace, but note that any missing dependent DLL would show up similarly:

这篇关于C DLL加载在C ++程序中,而不是在python Ctypes中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:01