Concerns about using Python's ctypes library on Windows

Adrian Buehlmann adrian at cadifra.com
Fri Jul 29 18:49:26 CDT 2011


(re adding list-cc)

On 2011-07-30 01:06, David Golub wrote:
>>> 6. What about switching to pure Win64 APIs? Does it require changing
>>>    exports from kernel32 into something like kernel64?
>>
>> I don't know what you mean by this. But I've just used Windows C API's
>> like GetConsoleCP as is documented at
>>
>> http://msdn.microsoft.com/en-us/library/ms683162(v=vs.85).aspx
>>
>> This function is available on Windows 2000 Professional and higher,
>> which includes 32 bit and 64bit versions. The documentation says in
>> what dll it is implemented (Kernel32.dll), which is enough for ctypes
>> to load it and interface to it.
> 
> On 64-bit versions of Windows, there two versions of each system DLL,
> including kernel32.dll.  One is stored in c:\windows\system32 and is used
> for 64-bit processes.  The other is stored in c:\windows\syswow64 and is
> used for 32-bit processes.  Windows will automatically load the correct
> version of the DLL depending on whether the application referencing it is
> 32-bit or 64-bit.

And depending on whether we use the x86 or x64 python.exe, we're for example
constructing correct x86 or x64 pointer-sized fields in structs
(ctypes.Structure):

$ python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> ctypes.sizeof(ctypes.c_void_p)
8

$ C:\Python26\python.exe
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> ctypes.sizeof(ctypes.c_void_p)
4

from win32.py:

_HANDLE = ctypes.c_void_p
_DWORD = ctypes.c_ulong

class _PROCESS_INFORMATION(ctypes.Structure):
    _fields_ = [('hProcess', _HANDLE),
                ('hThread', _HANDLE),
                ('dwProcessId', _DWORD),
                ('dwThreadId', _DWORD)]



More information about the Mercurial-devel mailing list