在 Windows 中,当我导入 ctypes 模块时,ctypes.cdll.msvcrt
对象会自动存在,它代表 msvcrt
Microsoft C++ 运行时库 根据文档。
但是,我注意到还有一个 find_msvcrt 函数,它将“返回Python使用的VC运行类型库的文件名”
。
它进一步指出,“如果您需要释放内存,例如,由扩展模块通过调用 free(void *) 分配的内存,那么在分配内存的同一库中使用该函数非常重要。”
所以我的问题是,我已有的 ctypes.cdll.msvcrt
库与我可以使用 find_msvcrt
加载的库之间有什么区别?功能?在什么特定情况下它们可能不是同一个库?
In Windows, the ctypes.cdll.msvcrt
object automatically exists when I import the ctypes module, and it represents the msvcrt
Microsoft C++ runtime library according to the docs.
However, I notice that there is also a find_msvcrt function which will "return the filename of the VC runtype library used by Python"
.
It further states, "If you need to free memory, for example, allocated by an extension module with a call to the free(void *), it is important that you use the function in the same library that allocated the memory."
So my question is, what's the difference between the ctypes.cdll.msvcrt
library that I already have and the one which I can load with the find_msvcrt
function? Under what specific circumstances might they not be the same library?
发布评论
评论(1)
不仅
ctypes.cdll.msvcrt
自动存在,而且ctypes.cdll.anything
自动存在,并且在第一次访问时加载,加载anything.dll< /代码>。因此,
ctypes.cdll.msvcrt
加载msvcrt.dll
,它是作为 Windows 一部分提供的库。 Python 链接的不是 C 运行时,因此您不应从msvcrt
调用 malloc/free。例如,对于 Python 2.6/3.1,您应该使用
ctypes.cdll.msvcr90
。由于这会随着时间的推移而改变,find_msvcrt()< /code>
为您提供应该真正使用的库的名称(然后通过
ctypes.CDLL
)。以下是 Microsoft CRT 的几个不同版本的名称,它们作为 MSC、VC++、平台 SDK 或 Windows 的一部分在不同时间点发布:crtdll.dll、msvcrt.dll、msvcrt4.dll、msvcr70.dll、msvcr71。 dll、msvcr80.dll、msvcr90.dll。
It's not just that
ctypes.cdll.msvcrt
automatically exists, butctypes.cdll.anything
automatically exists, and is loaded on first access, loadinganything.dll
. Soctypes.cdll.msvcrt
loadsmsvcrt.dll
, which is a library that ships as part of Windows. It is not the C runtime that Python links with, so you shouldn't call the malloc/free frommsvcrt
.For example, for Python 2.6/3.1, you should be using
ctypes.cdll.msvcr90
. As this will change over time,find_msvcrt()
gives you the name of the library that you should really use (and then load throughctypes.CDLL
).Here are the names of a few different versions of the Microsoft CRT, released at various points as part of MSC, VC++, the platform SDK, or Windows: crtdll.dll, msvcrt.dll, msvcrt4.dll, msvcr70.dll, msvcr71.dll, msvcr80.dll, msvcr90.dll.