これを行うために私が見つけた唯一の真に効果的な方法は、 と の呼び出しを担当することLoadLibrary
ですFreeLibrary
。このような:
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 with lib in 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)
アップデート:
Python 3.8 の時点で、ファイル名が渡されていないことを示すことをctypes.WinDLL()
受け入れなくなりました。None
代わりに、空の文字列を渡すことでこれを回避できます。
https://bugs.python.org/issue39243を参照してください