Ctypes Error: Why can't it find the DLL.

Discussion in 'Python' started by Mudcat, Oct 24, 2006.

  1. Mudcat

    Mudcat Guest

    Hi,

    I can't figure out why ctypes won't load the DLL I need to use. I've
    tried everything I can find (and the ctypes website is down at the
    moment). Here's what I've seen so far.

    I've added the file arapi51.dll to the system32 directory. However
    when I tried to access it I see this:

    >>> print windll.arapi51 # doctest: +WINDOWS

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 387, in
    __getattr__
    dll = self._dlltype(name)
    File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    __init__
    self._handle = _dlopen(self._name, mode)
    WindowsError: [Errno 126] The specified module could not be found

    So then I use the find_library function, and it finds it:

    >>> find_library('arapi51.dll')

    'C:\\WINNT\\system32\\arapi51.dll'

    At that point I try to use the LoadLibrary function, but it still can't
    find it:

    >>> windll.LoadLibrary('C:\WINNT\system32\arapi51.dll')

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 395, in
    LoadLibrary
    return self._dlltype(name)
    File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    __init__
    self._handle = _dlopen(self._name, mode)
    WindowsError: [Errno 126] The specified module could not be found

    What am I doing wrong? I've used ctypes before and not had this
    problem. Before, I've just added the file to the system32 directory and
    not had this problem.

    On another note, if I wanted to include these DLL's to be a part of an
    installable package, how would I configure ctypes to use DLL's from a
    local directory?

    Thanks,
    Marc
     
    Mudcat, Oct 24, 2006
    #1
    1. Advertising

  2. Mudcat a écrit :
    > Hi,
    >
    > I can't figure out why ctypes won't load the DLL I need to use. I've
    > tried everything I can find (and the ctypes website is down at the
    > moment). Here's what I've seen so far.
    >
    > I've added the file arapi51.dll to the system32 directory. However
    > when I tried to access it I see this:
    >

    (snip)
    > WindowsError: [Errno 126] The specified module could not be found
    >
    > So then I use the find_library function, and it finds it:

    (snip)
    > At that point I try to use the LoadLibrary function, but it still can't
    > find it:

    (snip)
    > WindowsError: [Errno 126] The specified module could not be found
    >
    > What am I doing wrong? I've used ctypes before and not had this
    > problem. Before, I've just added the file to the system32 directory and
    > not had this problem.


    Looks like it could have to do with another dll arapi51.dll depends upon
    that would be missing, cf:
    http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/2593616

    HTH
     
    Bruno Desthuilliers, Oct 25, 2006
    #2
    1. Advertising

  3. Mudcat

    Chetan Guest

    "Mudcat" <> writes:

    Is the DLL loadable from a standalone C program?
    All that is needed is a call to LoadLibrary() and check the return.

    If the full path is not specified, the dll is searched in the predefined order
    - I believe it is
    executable directory - in this case, directory of python.exe
    current directory - set the current drive and path to whereever you want
    before trying to load the dll
    windows system directory
    windows directory
    Directories specified in PATH evnvironment variable

    -Chetan

    > Hi,
    >
    > I can't figure out why ctypes won't load the DLL I need to use. I've
    > tried everything I can find (and the ctypes website is down at the
    > moment). Here's what I've seen so far.
    >
    > I've added the file arapi51.dll to the system32 directory. However
    > when I tried to access it I see this:
    >
    >>>> print windll.arapi51 # doctest: +WINDOWS

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 387, in
    > __getattr__
    > dll = self._dlltype(name)
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    > __init__
    > self._handle = _dlopen(self._name, mode)
    > WindowsError: [Errno 126] The specified module could not be found
    >
    > So then I use the find_library function, and it finds it:
    >
    >>>> find_library('arapi51.dll')

    > 'C:\\WINNT\\system32\\arapi51.dll'
    >
    > At that point I try to use the LoadLibrary function, but it still can't
    > find it:
    >
    >>>> windll.LoadLibrary('C:\WINNT\system32\arapi51.dll')

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 395, in
    > LoadLibrary
    > return self._dlltype(name)
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    > __init__
    > self._handle = _dlopen(self._name, mode)
    > WindowsError: [Errno 126] The specified module could not be found
    >
    > What am I doing wrong? I've used ctypes before and not had this
    > problem. Before, I've just added the file to the system32 directory and
    > not had this problem.
    >
    > On another note, if I wanted to include these DLL's to be a part of an
    > installable package, how would I configure ctypes to use DLL's from a
    > local directory?
    >
    > Thanks,
    > Marc
     
    Chetan, Oct 25, 2006
    #3
  4. Mudcat

    Jon Clements Guest

    Mudcat wrote:


    > So then I use the find_library function, and it finds it:
    >
    > >>> find_library('arapi51.dll')

    > 'C:\\WINNT\\system32\\arapi51.dll'
    >


    Notice it's escaped the '\' character.

    > At that point I try to use the LoadLibrary function, but it still can't
    > find it:
    >
    > >>> windll.LoadLibrary('C:\WINNT\system32\arapi51.dll')

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 395, in
    > LoadLibrary
    > return self._dlltype(name)
    > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    > __init__
    > self._handle = _dlopen(self._name, mode)
    > WindowsError: [Errno 126] The specified module could not be found
    >
    > What am I doing wrong? [snip]


    You need to use either
    windll.LoadLibrary(r'c:\winnt\system32\arapi51.dll') or escape the \'s
    as the find_library function did. You're getting caught out by \a which
    is the alert/bell character. \w and \s aren't valid escape sequences so
    you get away with them. I'm guessing it's worked before because you've
    been lucky.

    Works fine!:
    >>> 'C:\winnt\system32\smtpctrs.dll'

    'C:\\winnt\\system32\\smtpctrs.dll'

    Uh oh, escaped:
    >>> 'C:\winnt\system32\arapi51.dll'

    'C:\\winnt\\system32\x07rapi51.dll'

    Jon.
     
    Jon Clements, Oct 25, 2006
    #4
  5. Mudcat

    Chetan Guest

    "Jon Clements" <> writes:

    > Mudcat wrote:
    >
    >
    > > So then I use the find_library function, and it finds it:
    > >
    > > >>> find_library('arapi51.dll')

    > > 'C:\\WINNT\\system32\\arapi51.dll'
    > >

    >
    > Notice it's escaped the '\' character.
    >
    > > At that point I try to use the LoadLibrary function, but it still can't
    > > find it:
    > >
    > > >>> windll.LoadLibrary('C:\WINNT\system32\arapi51.dll')

    > > Traceback (most recent call last):
    > > File "<interactive input>", line 1, in ?
    > > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 395, in
    > > LoadLibrary
    > > return self._dlltype(name)
    > > File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 312, in
    > > __init__
    > > self._handle = _dlopen(self._name, mode)
    > > WindowsError: [Errno 126] The specified module could not be found
    > >
    > > What am I doing wrong? [snip]

    >
    > You need to use either
    > windll.LoadLibrary(r'c:\winnt\system32\arapi51.dll') or escape the \'s
    > as the find_library function did. You're getting caught out by \a which
    > is the alert/bell character. \w and \s aren't valid escape sequences so
    > you get away with them. I'm guessing it's worked before because you've
    > been lucky.
    >
    > Works fine!:
    > >>> 'C:\winnt\system32\smtpctrs.dll'

    > 'C:\\winnt\\system32\\smtpctrs.dll'
    >
    > Uh oh, escaped:
    > >>> 'C:\winnt\system32\arapi51.dll'

    > 'C:\\winnt\\system32\x07rapi51.dll'

    I wondered about it, but that would not explain why it fails in the first case
    - perhaps the OP now knows what happened. Interestingly I get a different
    error with the distribution binary for 2.5 - I get "Error 22" instead of "Errno
    126", but the message text was same, so I assumed this was from a different
    version and something had changed about how the errno is reported.

    However, I checked the windows error codes and 126 seems to be the correct
    error. The error code talks about bad command and the corresponding message
    text reads "The device does not recognize the command."

    -Chetan

    >
    > Jon.
     
    Chetan, Oct 25, 2006
    #5
  6. Mudcat

    Mudcat Guest

    That was it. Once I added the other DLLs then it was able to find and
    make the call.

    Thanks for all the help,
    Marc


    Bruno Desthuilliers wrote:
    > Mudcat a écrit :
    > > Hi,
    > >
    > > I can't figure out why ctypes won't load the DLL I need to use. I've
    > > tried everything I can find (and the ctypes website is down at the
    > > moment). Here's what I've seen so far.
    > >
    > > I've added the file arapi51.dll to the system32 directory. However
    > > when I tried to access it I see this:
    > >

    > (snip)
    > > WindowsError: [Errno 126] The specified module could not be found
    > >
    > > So then I use the find_library function, and it finds it:

    > (snip)
    > > At that point I try to use the LoadLibrary function, but it still can't
    > > find it:

    > (snip)
    > > WindowsError: [Errno 126] The specified module could not be found
    > >
    > > What am I doing wrong? I've used ctypes before and not had this
    > > problem. Before, I've just added the file to the system32 directory and
    > > not had this problem.

    >
    > Looks like it could have to do with another dll arapi51.dll depends upon
    > that would be missing, cf:
    > http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/2593616
    >
    > HTH
     
    Mudcat, Oct 28, 2006
    #6
  7. Mudcat a écrit :
    > That was it. Once I added the other DLLs then it was able to find and
    > make the call.
    >
    > Thanks for all the help,


    I just googled for "WindowsError: [Errno 126]" and followed the links,
    you know...
     
    Bruno Desthuilliers, Oct 31, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Fields
    Replies:
    0
    Views:
    2,840
    Sam Fields
    Oct 22, 2004
  2. Henk Punt
    Replies:
    0
    Views:
    411
    Henk Punt
    Jul 23, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,090
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    0
    Views:
    519
  5. H. Simpson
    Replies:
    4
    Views:
    311
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page