I’m trying to access some functions in a dll (nss3.dll) that ships with Firefox web browser. To handle this task I have used ctypes in Python. The problem is that it fails at the initial point which is when loading the dll in to the memory.
This is the code snippet that I have to do so.
>>> from ctypes import *
>>> windll.LoadLibrary("E:\\nss3.dll")
The exception I’m getting is
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
windll.LoadLibrary("E:\\nss3.dll")
File "C:\Python26\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
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
I also tried loading it from the Firefox installation path assuming that there maybe dependencies.
>>> windll.LoadLibrary("F:\\Softwares\\Mozilla Firefox\\nss3.dll")
But I’m getting the same exception as mentioned above.
Thanks.
nss3.dll is linked to the following DLLs, which are all located in the Firefox directory: nssutil3.dll, plc4.dll, plds4.dll, nspr4.dll, and mozcrt19.dll. The system library loader looks for these files in the DLL search path of the process, which includes the application directory, system directories, the current directory, and each of the directories listed in the
PATHenvironment variable.The simplest solution is to change the current directory to the DLL Firefox directory. However, that’s not thread safe, so I wouldn’t rely on it in general. Another option is to append the Firefox directory to the
PATHenvironment variable, which is what I suggested in my original version of this answer. However, that’s not much better than modifying the current directory.Newer versions of Windows (NT 6.0+ with update KB2533623) allow the DLL search path to be updated in a thread-safe manner via
SetDefaultDllDirectories,AddDllDirectory, andRemoveDllDirectory. But that approach would be over the top here.In this case, for the sake of both simplicity and compatibility with older versions of Windows, it suffices to call
LoadLibraryExwith the flagLOAD_WITH_ALTERED_SEARCH_PATH. You need to load the DLL using an absolute path, else the behavior is undefined. For convenience we can subclassctypes.CDLLandctypes.WinDLLto callLoadLibraryExinstead ofLoadLibrary.Here are all of the available
LoadLibraryExflags:For example: