[Python-3000] Ctypes as cross-interpreter C calling interface

Paul Prescod paul at prescod.net
Fri Aug 11 01:57:59 CEST 2006


And if you're curious about how to use ctypes without all of the helper
functions set up for you, then I guess it is easiest to poke around the
documentation for code samples.


>>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
>>> printf("String '%s', Int %d, Double %f\n", "Hi", 10, 2.2)
String 'Hi', Int 10, Double 2.200000
37
>>>


>>> from ctypes import c_int, WINFUNCTYPE, windll
>>> from ctypes.wintypes import HWND, LPCSTR, UINT
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, c_uint)
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption",
None), (1, "flags", 0)
>>> MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

It's ugly but in the typical case you would hide all of the declarations in
a module (maybe an auto-generated module) and just focus on your logic:

>>> MessageBox()
>>> MessageBox(text="Spam, spam, spam")
>>> MessageBox(flags=2, text="foo bar")
>>


 Paul Prescod
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20060810/07616e7c/attachment.htm 


More information about the Python-3000 mailing list