代码之家  ›  专栏  ›  技术社区  ›  Eli Courtwright

不同类型的msvcrt

  •  7
  • Eli Courtwright  · 技术社区  · 15 年前

    在Windows中, ctypes.cdll.msvcrt 对象在导入cTypes模块时自动存在,它表示 msvcrt 微软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 功能?在什么具体情况下,它们可能不是同一个库?

    1 回复  |  直到 15 年前
        1
  •  10
  •   Martin v. Löwis    15 年前

    不仅如此 ctypes.cdll.msvcrt 自动存在,但是 ctypes.cdll.anything 自动存在,并在第一次访问时加载,加载 anything.dll . 所以 cTypes.cdll.msvcrt类型 荷载 msvcrt.dll ,这是作为Windows的一部分提供的库。python链接的不是c运行时,因此不应该调用malloc/free from msvcrt .

    例如,对于Python2.6/3.1,您应该使用 ctypes.cdll.msvcr90 . 因为这会随着时间的推移而改变, find_msvcrt() 为您提供真正应该使用的库的名称(然后加载到 ctypes.CDLL )

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