我正在加载一个带有如下ctype的dll:
lib = cdll.LoadLibrary("someDll.dll");当我用完这个库时,我需要卸载它以释放它使用的资源。我在文档中找到任何关于如何做到这一点的东西都有问题。我看到一个相当古老的帖子:How can I unload a DLL using ctypes in Python?。我希望有一些明显的东西,我没有发现,而不是黑客。
发布于 2012-10-30 04:31:21
我找到的唯一真正有效的方法是负责调用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)更新:
从Python3.8开始,ctypes.WinDLL()不再接受None来表示没有传递文件名。相反,您可以通过传递空字符串来解决此问题。
https://stackoverflow.com/questions/13128995
复制相似问题