ctypes卸载DLL

Doo*_*Dah 16 python ctypes

我正在加载一个像这样的ctypes的DLL:

lib = cdll.LoadLibrary("someDll.dll");
Run Code Online (Sandbox Code Playgroud)

当我完成库,我需要卸载它以释放它使用的资源.我在文档中找到有关如何执行此操作的任何内容时遇到问题.我看到这个相当古老的帖子:如何在Python中使用ctypes卸载DLL?.我希望有一些显而易见的东西,我没有找到,而不是一个黑客.

Dav*_*nan 22

我所发现的唯一真正有效的方法是负责召唤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)
Run Code Online (Sandbox Code Playgroud)