【问题标题】:Different versions of msvcrt in ctypesctypes中不同版本的msvcrt
【发布时间】:2010-11-23 19:53:18
【问题描述】:

在 Windows 中,当我导入 ctypes 模块时,ctypes.cdll.msvcrt 对象自动存在,它代表msvcrt Microsoft C++ 运行时库according to the docs。

但是,我注意到还有一个 find_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 函数加载的库之间有什么区别?在什么特定情况下它们可能不是同一个库?

【问题讨论】:

标签: python ctypes msvcrt


【解决方案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() 为您提供了您应该真正使用的库的名称(然后通过 ctypes.CDLL 加载)。

以下是 Microsoft CRT 的几个不同版本的名称,它们作为 MSC、VC++、平台 SDK 或 Windows 的一部分在不同时间发布:crtdll.dll、msvcrt.dll、msvcrt4.dll、msvcr70.dll , msvcr71.dll, msvcr80.dll, msvcr90.dll。

【讨论】:

  • 谢谢!我没有花太多时间在 Windows 领域进行编程,所以这种细节非常有帮助。
  • 为了完整起见 - ctypes.cdll[ctypes.util.find_msvcrt()] 应该返回正确 CRT 的句柄。
猜你喜欢
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
相关资源
最近更新 更多