C DLL loads in C++ program, not in python Ctypes

2020-01-29 20:49发布

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.

1条回答
女痞
2楼-- · 2020-01-29 21:24

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

Process Monitor Filter dialog

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:

Process Monitor trace

查看更多
登录 后发表回答