Python ctypes.WinDLL error , _dlopen(self._name, mode) can't be found

wizztjh picture wizztjh · Jun 23, 2010 · Viewed 18.2k times · Source
ctypes.WinDLL("C:\Program Files\AHSDK\bin\ahscript.dll")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

How can I solve it? I found the _dlopen in C:\Python26\lib\ctypes\__init__.py, but I really don't know how to solve it.

Answer

Daniel Stutzbach picture Daniel Stutzbach · Jun 23, 2010

Backslashes are an escape character within strings, as demonstrated in the example below:

>>> print "C:\Program Files\AHSDK\bin\ahscript.dll"
C:\Program Files\AHSDinhscript.dll

You can solve the problem by placing an r before the string, which prevents the backslash from working as an escape character:

ctypes.WinDLL(r"C:\Program Files\AHSDK\bin\ahscript.dll")

Alternately, you could escape the backslashes:

ctypes.WinDLL("C:\\Program Files\\AHSDK\\bin\\ahscript.dll")