Skip to content Skip to sidebar Skip to footer

Ctypes Unload Dll

I am loading a dll with ctypes like this: lib = cdll.LoadLibrary('someDll.dll'); When I am done with the library, I need to unload it to free resources it uses. I am having probl

Solution 1:

The only truly effective way I have ever found to do this is to take charge of calling LoadLibrary and FreeLibrary. Like this:

import ctypes

# get the module handle and create a ctypes library object
libHandle = ctypes.windll.kernel32.LoadLibraryA('mydll.dll')lib = ctypes.WinDLL(None, handle=libHandle)

# do stuff withlibin the usual way
lib.Foo(42, 666)

# clean up by removing reference to the ctypes library object
del lib

# unload the DLL
ctypes.windll.kernel32.FreeLibrary(libHandle)

Update:

As of Python 3.8, ctypes.WinDLL() no longer accepts None to indicate that no filename is being passed. Instead, you can workaround this by passing an empty string.

See https://bugs.python.org/issue39243

Post a Comment for "Ctypes Unload Dll"