Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - WindowsError: [Error 126] when loading a DLL with ctypes

This works fine on Windows 7 with Python 2.7:

lib = ctypes.cdll.LoadLibrary('prov_means')
provmeans = lib.provmeans  

The library prov_means.DLL is in my working directory. It exports a simple, stand-alone C function provmeans() with no dependencies.

When I try the same thing on Windows XP and Python 2.7 I get

Traceback (most recent call last):
  File "D:pythonAuxilsrcauxil.py", line 130, in <module>
    lib = ctypes.cdll.LoadLibrary('prov_means')
  File "C:Python27libctypes\__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "C:Python27libctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found 

I have tried copying the DLL to WindowsSystem32 and also entering the full path name

"d:\python\auxil\src\prov_means"

with and without the ".DLL" extension. Nothing works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Error 126 is what you get when a dependent DLL can not be found. There are two obvious causes for this:

  1. Your DLL is not being located.
  2. Your DLL depends on other DLLs that cannot be found.

I doubt that option 1 is the problem but in any case I think I would probably be using a full path to that DLL to be sure.

So that leaves option 2 and the most common cause for that is that your target machine does not have the C++ runtime installed. Either install the C++ runtime on your target machine, or use static linking, /MT, when building your DLL so that you do not need to redistribute the runtime.

Probably, on the machine that you developed the DLL, you have installed a C++ compiler and that installed the runtime for you. On your target machine, where the code fails, you have not installed the compiler and so the runtime is not present.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...