题
在 Windows 中, ctypes.cdll.msvcrt
当我导入 ctypes 模块时,对象自动存在,它代表 msvcrt
微软C++运行时库 根据文档.
不过,我注意到还有一个 查找msvcrt 函数将 "return the filename of the VC runtype library used by Python"
.
它进一步指出, "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."
所以我的问题是,两者有什么区别 ctypes.cdll.msvcrt
我已经拥有的库以及我可以使用 find_msvcrt
功能?在什么特定情况下它们可能不是同一个库?
解决方案
不仅如此 ctypes.cdll.msvcrt
自动存在,但是 ctypes.cdll.anything
自动存在,并在第一次访问时加载,正在加载 anything.dll
. 。所以 ctypes.cdll.msvcrt
负载 msvcrt.dll
, ,它是作为 Windows 的一部分提供的库。Python 链接的不是 C 运行时,因此您不应该从中调用 malloc/free msvcrt
.
例如,对于 Python 2.6/3.1,您应该使用 ctypes.cdll.msvcr90
. 。由于这会随着时间的推移而改变, find_msvcrt()
为您提供您应该真正使用的库的名称(然后加载 ctypes.CDLL
).
以下是 Microsoft CRT 的几个不同版本的名称,它们作为 MSC、VC++、平台 SDK 或 Windows 的一部分在不同的时间点发布:crtdll.dll、msvcrt.dll、msvcrt4.dll、msvcr70.dll、msvcr71.dll、msvcr80.dll、msvcr90.dll。