Different versions of msvcrt in ctypes

On Windows, the ctypes.cdll.msvcrt object automatically exists when I import the ctypes module and it represents the Microsoft C ++ msvcrt library according to the docs .

However, I noticed that there is a find_msvcrt function that will be "return the filename of the VC runtype library used by Python" .

The following reads, "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 is the difference between the ctypes.cdll.msvcrt library that I already have and the one that I can load with the find_msvcrt function? Under what circumstances can they not be the same library?

+7
python msvcrt ctypes
source share
1 answer

It is not only that ctypes.cdll.msvcrt automatically exists, but ctypes.cdll.anything automatically exists and loads on first access, loading anything.dll . Therefore, ctypes.cdll.msvcrt loads msvcrt.dll , which is the library that ships as part of Windows. This is not a C runtime environment that Python works with, so you should not call malloc / free from msvcrt .

For example, for Python 2.6 / 3.1 you should use ctypes.cdll.msvcr90 . Since this will change over time, find_msvcrt() gives you the name of the library that you really should use (and then load through ctypes.CDLL ).

The following are the names of several different versions of Microsoft CRT released at different points as part of the MSC, VC ++, platform SDK or Windows: crtdll.dll, msvcrt.dll, msvcrt4.dll, msvcr70.dll, msvcr71.dll, msvcr80.dll, msvcr90.dll.

+10
source share

All Articles