I found the code I wrote some time ago for the same purpose:
(in pypy, which uses ctypes a lot)
import sys
if sys.platform == 'win32':
# Parses sys.version and deduces the version of the compiler
import distutils.msvccompiler
version = distutils.msvccompiler.get_build_version()
if version is None:
# This logic works with official builds of Python.
if sys.version_info < (2, 4):
clibname = 'msvcrt'
else:
clibname = 'msvcr71'
else:
if version <= 6:
clibname = 'msvcrt'
else:
clibname = 'msvcr%d' % (version * 10)
# If python was built with in debug mode
import imp
if imp.get_suffixes()[0][0] == '_d.pyd':
clibname += 'd'
standard_c_lib = ctypes.cdll.LoadLibrary(clibname+'.dll')
This code works on all pythons I have on my machine: official builds,
custom builds (relase/debug) with several MS compilers...
I did not test it with other compiled vendors (mingw32...).
But to me this seems more robust than a text search in the executable.