gpt4 book ai didi

python - 使用 ctypes windll 卸载 64 位 dll 时出错

转载 作者:太空狗 更新时间:2023-10-30 02:20:24 27 4
gpt4 key购买 nike

我发现这里有几篇关于使用 ctypes 卸载 dll 的帖子,我完全按照据说有效的方式进行操作从 ctypes 导入 *

file = CDLL('file.dll')

# do some stuff here

handle = file._handle # obtain the DLL handle

windll.kernel32.FreeLibrary(handle)

然而,我在 python 64 位上,我的 dll 也是为 x64 编译的,我从上面的最后一行得到了一个错误:

argument 1: <class 'OverflowError'>: int too long to convert

而且我检查句柄是一个 long int (int64) of '8791681138688',那么这是否意味着 windll.kernel32 只处理 int32 句柄? Google 搜索显示 kernal32 也适用于 64 位 Windows。那我应该怎么处理呢?

最佳答案

FreeLibrary接受一个句柄,定义为 C void * 指针。引用Windows Data Types .在函数指针的 argtypes 中设置:

import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
kernel32.FreeLibrary.argtypes = [wintypes.HMODULE]

Python intlong(在 Python 3 中重命名为 int)的默认转换是 C long,随后被转换为 C int。 Microsoft 即使在 64 位 Windows 上也使用 32 位 long,这就是转换引发 OverflowError 的原因。

在具有 64 位 long 的平台上(即几乎所有其他 64 位操作系统),将指针作为 Python 整数传递而不定义函数的 argtypes实际上可能会对该过程进行段错误。 long 的初始转换工作正常,因为它与指针的大小相同。但是,随后转换为 32 位 C int 可能会默默地截断该值。

关于python - 使用 ctypes windll 卸载 64 位 dll 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23522055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com