ctypes卸载dll [英] ctypes unload dll

查看:114
本文介绍了ctypes卸载dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载具有以下ctypes的dll:

I am loading a dll with ctypes like this:

lib = cdll.LoadLibrary("someDll.dll");

对库完成后,需要将其卸载以释放其使用的资源。我在查找文档中有关如何执行此操作时遇到问题。我看到了这个比较老的帖子:如何卸载在Python中使用ctypes的DLL吗?。我希望有一些我没有发现的显而易见的东西,并且很少被黑客入侵。

When I am done with the library, I need to unload it to free resources it uses. I am having problems finding anything in the docs regarding how to do this. I see this rather old post: How can I unload a DLL using ctypes in Python?. I am hoping there is something obvious I have not found and less of a hack.

推荐答案

我唯一真正有效的方法被发现这样做是负责调用 LoadLibrary FreeLibrary 。像这样:

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 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 表示未传递文件名。而是可以通过传递空字符串来解决此问题。

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.

请参见 https://bugs.python.org/issue39243

这篇关于ctypes卸载dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆