Changed in Version 3.3 - Windows e
Changed in Version 3.3 - Windows e
Note: The code samples in this tutorial use doctest to make sure that they actually
work. Since some code samples behave differently under Linux, Windows, or
macOS, they contain doctest directives in comments.
Note: Some code samples reference the ctypes c_int type. On platforms where
sizeof(long) == sizeof(int) it is an alias to c_long. So, you should not be
confused if c_long is printed if you would expect c_int — they are actually the
same type.
ctypes exports the cdll, and on Windows windll and oledll objects, for loading
dynamic link libraries.
You load libraries by accessing them as attributes of these objects. cdll loads
libraries which export functions using the standard cdecl calling convention, while
windll libraries call functions using the stdcall calling convention. oledll also uses
the stdcall calling convention, and assumes the functions return a Windows
HRESULT error code. The error code is used to automatically raise an OSError
exception when the function call fails.
Changed in version 3.3: Windows errors used to raise WindowsError, which is now
an alias of OSError.
Here are some examples for Windows. Note that msvcrt is the MS standard C
library containing most standard C functions, and uses the cdecl calling convention:
>>>
>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>
>>> print(cdll.msvcrt)
>>>
>>>
>>> cdll.LoadLibrary("libc.so.6")
>>>
>>>
>>> libc.printf
>>> print(windll.kernel32.GetModuleHandleA)
>>> print(windll.kernel32.MyOwnFunction)
>>>
Note that win32 system dlls like kernel32 and user32 often export ANSI as well as
UNICODE versions of a function. The UNICODE version is exported with an W
appended to the name, while the ANSI version is exported with an A appended to the
name. The win32 GetModuleHandle function, which returns a