overview :: tutorial :: reference :: faq
( Work in progress: COM :: COM sample )
PyHandleto a dll function?
win32all functions often return a
PyHandle object, for
ctypes has it's own
protocol to convert Python objects into C parameters when it
calls functions loaded from a dll, so it does not know how to
PyHandle object to the C function.
PyHandle objects have a
.handle attribute which is an
integer, and this can be passed to a function by
h = win32file.CreateFile(....) windll.kernel32.DeviceIoControl(h.handle, ...)
Another possibility is to convert the
PyHandle object into an
integer by writing the call in this way:
h = win32file.CreateFile(....) windll.kernel32.DeviceIoControl(int(h), ...)
Delphi uses the
PASCAL calling convention as default, and
this expects the parameters in reverse order. Write the
arguments in reverse order in the call, and it should work when
using the __stdcall calling convention (load the Delphi dll
New in 0.6.1:
ctypes types now have a
method, which accepts a dll/shared library instance and a
symbol name as parameters. See the tutorial for details.
ctypes loads dlls by retrieving them as attributes from
windll.gdi32 which loads
gdi32.dll. This approach cannot work when the filename
contains characters not allowed in Python identifiers, or when
the dll is not on the default Windows search path. In these
cases you should call the
WinDLL classes directly
with the filename:
gpib32 = CDLL("c:\\gpib\\gpib-32.dll")
Jimmy Retzlaff explains how the C++ compiler mangles function names and how it can be avoided in this post to the ctypes-users mailing list.
Send new questions, better answers, or other comments to the ctypes-users mailing list.