How can I unload a DLL using ctypes in Python?

Solution 1:

you should be able to do it by disposing the object

mydll = ctypes.CDLL('...')
del mydll
mydll = ctypes.CDLL('...')

EDIT: Hop's comment is right, this unbinds the name, but garbage collection doesn't happen that quickly, in fact I even doubt it even releases the loaded library.

Ctypes doesn't seem to provide a clean way to release resources, it does only provide a _handle field to the dlopen handle...

So the only way I see, a really, really non-clean way, is to system dependently dlclose the handle, but it is very very unclean, as moreover ctypes keeps internally references to this handle. So unloading takes something of the form:

mydll = ctypes.CDLL('./mylib.so')
handle = mydll._handle
del mydll
while isLoaded('./mylib.so'):
    dlclose(handle)

It's so unclean that I only checked it works using:

def isLoaded(lib):
   libp = os.path.abspath(lib)
   ret = os.system("lsof -p %d | grep %s > /dev/null" % (os.getpid(), libp))
   return (ret == 0)

def dlclose(handle)
   libdl = ctypes.CDLL("libdl.so")
   libdl.dlclose(handle)

Solution 2:

It is helpful to be able to unload the DLL so that you can rebuild the DLL without having to restart the session if you are using iPython or similar work flow. Working in windows I have only attempted to work with the windows DLL related methods.

REBUILD = True
if REBUILD:
  from subprocess import call
  call('g++ -c -DBUILDING_EXAMPLE_DLL test.cpp')
  call('g++ -shared -o test.dll test.o -Wl,--out-implib,test.a')

import ctypes
import numpy

# Simplest way to load the DLL
mydll = ctypes.cdll.LoadLibrary('test.dll')

# Call a function in the DLL
print mydll.test(10)

# Unload the DLL so that it can be rebuilt
libHandle = mydll._handle
del mydll
ctypes.windll.kernel32.FreeLibrary(libHandle)

I don't know much of the internals so I'm not really sure how clean this is. I think that deleting mydll releases the Python resources and the FreeLibrary call tells windows to free it. I had assumed that freeing with FreeLibary first would have produced problems so I saved a copy of the library handle and freed it in the order shown in the example.

I based this method on ctypes unload dll which loaded the handle explicitly up front. The loading convention however does not work as cleanly as the simple "ctypes.cdll.LoadLibrary('test.dll')" so I opted for the method shown.

Solution 3:

Piotr's answer helped me, but I did run into one issue on 64-bit Windows:

Traceback (most recent call last):
...
ctypes.ArgumentError: argument 1: <class 'OverflowError'>: int too long to convert

Adjusting the argument type of the FreeLibrary call as suggested in this answer solved this for me.

Thus we arrive at the following complete solution:

import ctypes, ctypes.windll

def free_library(handle):
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
    kernel32.FreeLibrary.argtypes = [ctypes.wintypes.HMODULE]
    kernel32.FreeLibrary(handle)

Usage:

lib = ctypes.CDLL("foobar.dll")
free_library(lib._handle)

Solution 4:

If you need this functionality, you could write 2 dlls where dll_A loads/Unloads library from dll_B. Use dll_A as as python interface-loader and passthrough for functions in dll_B.

Solution 5:

For total cross-compatibility: I maintain a list of various dlclose() equivalents for each platform and which library to get them from. It's a bit of a long list but feel free to just copy/paste it.

import sys
import ctypes
import platform

OS = platform.system()

if OS == "Windows":  # pragma: Windows
    dll_close = ctypes.windll.kernel32.FreeLibrary

elif OS == "Darwin":
    try:
        try:
            # macOS 11 (Big Sur). Possibly also later macOS 10s.
            stdlib = ctypes.CDLL("libc.dylib")
        except OSError:
            stdlib = ctypes.CDLL("libSystem")
    except OSError:
        # Older macOSs. Not only is the name inconsistent but it's
        # not even in PATH.
        stdlib = ctypes.CDLL("/usr/lib/system/libsystem_c.dylib")
    dll_close = stdlib.dlclose

elif OS == "Linux":
    try:
        stdlib = ctypes.CDLL("")
    except OSError:
        # Alpine Linux.
        stdlib = ctypes.CDLL("libc.so")
    dll_close = stdlib.dlclose

elif sys.platform == "msys":
    # msys can also use `ctypes.CDLL("kernel32.dll").FreeLibrary()`. Not sure
    # if or what the difference is.
    stdlib = ctypes.CDLL("msys-2.0.dll")
    dll_close = stdlib.dlclose

elif sys.platform == "cygwin":
    stdlib = ctypes.CDLL("cygwin1.dll")
    dll_close = stdlib.dlclose

elif OS == "FreeBSD":
    # FreeBSD uses `/usr/lib/libc.so.7` where `7` is another version number.
    # It is not in PATH but using its name instead of its path is somehow the
    # only way to open it. The name must include the .so.7 suffix.
    stdlib = ctypes.CDLL("libc.so.7")
    dll_close = stdlib.close

else:
    raise NotImplementedError("Unknown platform.")

dll_close.argtypes = [ctypes.c_void_p]

You can then use dll_close(dll._handle) to unload a library dll = ctypes.CDLL("your-library").

This list is taken from this file. I will update the master branch every time I encounter a new platform.