0% found this document useful (0 votes)
9 views4 pages

Changed in Version 3.3 - Windows e

This tutorial covers the use of ctypes for loading dynamic link libraries (DLLs) in Python, detailing the differences between loading methods on Windows and Linux. It explains how to access functions from loaded DLLs and highlights the importance of using the correct calling conventions and error handling. Additionally, it notes that accessing the standard C library on Windows may lead to compatibility issues with Python's version.

Uploaded by

diegohazael2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Changed in Version 3.3 - Windows e

This tutorial covers the use of ctypes for loading dynamic link libraries (DLLs) in Python, detailing the differences between loading methods on Windows and Linux. It explains how to access functions from loaded DLLs and highlights the importance of using the correct calling conventions and error handling. Additionally, it notes that accessing the standard C library on Windows may lead to compatibility issues with Python's version.

Uploaded by

diegohazael2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ctypes tutorial

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.

Loading dynamic link libraries

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:

>>>

>>> from ctypes import *

>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>

>>> print(cdll.msvcrt)

<CDLL 'msvcrt', handle ... at ...>

>>> libc = cdll.msvcrt

>>>

Windows appends the usual .dll file suffix automatically.

Note Accessing the standard C library through cdll.msvcrt will use an


outdated version of the library that may be incompatible with the one being used
by Python. Where possible, use native Python functionality, or else import and
use the msvcrt module.

On Linux, it is required to specify the filename including the extension to load a


library, so attribute access can not be used to load libraries. Either the
LoadLibrary() method of the dll loaders should be used, or you should load the
library by creating an instance of CDLL by calling the constructor:

>>>

>>> cdll.LoadLibrary("libc.so.6")

<CDLL 'libc.so.6', handle ... at ...>

>>> libc = CDLL("libc.so.6")


>>> libc

<CDLL 'libc.so.6', handle ... at ...>

>>>

Accessing functions from loaded dlls

Functions are accessed as attributes of dll objects:

>>>

>>> from ctypes import *

>>> libc.printf

<_FuncPtr object at 0x...>

>>> print(windll.kernel32.GetModuleHandleA)

<_FuncPtr object at 0x...>

>>> print(windll.kernel32.MyOwnFunction)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>


File "ctypes.py", line 239, in __getattr__

func = _StdcallFuncPtr(name, self)

AttributeError: function 'MyOwnFunction' not found

>>>

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

You might also like