Python | accessing dll using ctypes Python | accessing dll using ctypes python python

Python | accessing dll using ctypes


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 PATH environment 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 PATH environment 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, and RemoveDllDirectory. 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 LoadLibraryEx with the flag LOAD_WITH_ALTERED_SEARCH_PATH. You need to load the DLL using an absolute path, else the behavior is undefined. For convenience we can subclass ctypes.CDLL and ctypes.WinDLL to call LoadLibraryEx instead of LoadLibrary.

import osimport ctypesif os.name == 'nt':    from ctypes import wintypes    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)    def check_bool(result, func, args):        if not result:            raise ctypes.WinError(ctypes.get_last_error())        return args    kernel32.LoadLibraryExW.errcheck = check_bool    kernel32.LoadLibraryExW.restype = wintypes.HMODULE    kernel32.LoadLibraryExW.argtypes = (wintypes.LPCWSTR,                                        wintypes.HANDLE,                                        wintypes.DWORD)class CDLLEx(ctypes.CDLL):    def __init__(self, name, mode=0, handle=None,                  use_errno=True, use_last_error=False):        if os.name == 'nt' and handle is None:            handle = kernel32.LoadLibraryExW(name, None, mode)        super(CDLLEx, self).__init__(name, mode, handle,                                     use_errno, use_last_error)class WinDLLEx(ctypes.WinDLL):    def __init__(self, name, mode=0, handle=None,                  use_errno=False, use_last_error=True):        if os.name == 'nt' and handle is None:            handle = kernel32.LoadLibraryExW(name, None, mode)        super(WinDLLEx, self).__init__(name, mode, handle,                                       use_errno, use_last_error)

Here are all of the available LoadLibraryEx flags:

DONT_RESOLVE_DLL_REFERENCES         = 0x00000001LOAD_LIBRARY_AS_DATAFILE            = 0x00000002LOAD_WITH_ALTERED_SEARCH_PATH       = 0x00000008LOAD_IGNORE_CODE_AUTHZ_LEVEL        = 0x00000010  # NT 6.1LOAD_LIBRARY_AS_IMAGE_RESOURCE      = 0x00000020  # NT 6.0LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE  = 0x00000040  # NT 6.0# These cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.# Install update KB2533623 for NT 6.0 & 6.1.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR    = 0x00000100LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200LOAD_LIBRARY_SEARCH_USER_DIRS       = 0x00000400LOAD_LIBRARY_SEARCH_SYSTEM32        = 0x00000800LOAD_LIBRARY_SEARCH_DEFAULT_DIRS    = 0x00001000

For example:

firefox_path = r'F:\Softwares\Mozilla Firefox'nss3 = CDLLEx(os.path.join(firefox_path, 'nss3.dll'),               LOAD_WITH_ALTERED_SEARCH_PATH)nss3.NSS_GetVersion.restype = c_char_p>>> nss3.NSS_GetVersion()                 '3.13.5.0 Basic ECC'


Note that the ctypes module works with C extensions; if you want to write code in C++, you might do as follows (the C code is the same):

Your dll.c source: (you can use C++ code with .cpp extension without any problem)

#include <math.h>#ifdef __cplusplusextern "C" {#endif__declspec(dllexport) double _sin(double x){     return sin(x)}#ifdef __cplusplus}#endif

Command prompt with Administrator authentication:

With C source:

C:\Windows\system32>cl /LD "your_source_path\dll.c" /I "c:\Python33 \include" "c:\Python33\libs\python33.lib" /link/out:dll.dll

With C++ source:

C:\Windows\system32>cl /LD "your_source_path\dll.cpp" /I "c:\Python33 \include" "c:\Python33\libs\python33.lib" /link/out:dll.dll

Compiler generates DLL file:

Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86Copyright (C) Microsoft Corporation.  All rights reserved.dll.c // or dll.cpp Microsoft (R) Incremental Linker Version 11.00.50727.1Copyright (C) Microsoft Corporation.  All rights reserved./out:dll.dll/dll/implib:dll.lib/out:dll.dlldll.objc:\Python33\libs\python33.lib   Creating library dll.lib and object dll.exp

Your Python module:

import ctypesdll = ctypes.CDLL('your_dll_path')dll._sin.argtypes = [ctypes.c_double]dll._sin.restype = ctypes.c_doubleprint(dll._sin(34))# return 0.5290826861200238


I just had a similar problem with ctypes.CDLL, and I got it to work changing the current directory to the library directory and loading the library only by name (I guess putting the directory in the system path would work too)So, instead of

CDLL('C:/library/path/library.dll')

I did

os.chdir('C:/library/path')CDLL('library')