# `ctypes` --- A foreign function library for Python

Source code: Lib/ctypes

`ctypes` is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see Requirements for optional modules.

## Didacticiel de ctypes

Remarque : le type `c_int` du module apparaît dans certains de ces exemples. Sur les plates-formes où `sizeof(long) == sizeof(int)`, ce type est un alias de `c_long`. Ne soyez donc pas surpris si `c_long` s'affiche là où vous vous attendiez à `c_int` — il s'agit bien du même type.

### Chargement des DLL

`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.

Modifié dans la version 3.3: En Windows, les erreurs levaient auparavant une `WindowsError`, qui est maintenant un alias de `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 ajoute le suffixe habituel `.dll` automatiquement.

Note

Accéder à la bibliothèque standard C par `cdll.msvcrt` utilise une version obsolète de la bibliothèque qui peut avoir des problèmes de compatibilité avec celle que Python utilise. Si possible, mieux vaut utiliser la fonctionnalité native de Python, ou bien importer et utiliser le module `msvcrt`.

Other systems require 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.

For example, on Linux:

    >>> cdll.LoadLibrary("libc.so.6")
    <CDLL 'libc.so.6', handle ... at ...>
    >>> libc = CDLL("libc.so.6")
    >>> libc
    <CDLL 'libc.so.6', handle ... at ...>
    >>>

On macOS:

    >>> cdll.LoadLibrary("libc.dylib")
    <CDLL 'libc.dylib', handle ... at ...>
    >>> libc = CDLL("libc.dylib")
    >>> libc
    <CDLL 'libc.dylib', handle ... at ...>

### Accès aux fonctions des DLL chargées

Les fonctions sont alors des attributs des objets DLL :

    >>> 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 a `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 module handle for a given module name, has the following C prototype, and a macro is used to expose one of them as `GetModuleHandle` depending on whether UNICODE is defined or not:

    /* ANSI version */
    HMODULE GetModuleHandleA(LPCSTR lpModuleName);
    /* UNICODE version */
    HMODULE GetModuleHandleW(LPCWSTR lpModuleName);

windll n'en choisit pas une par magie, il faut accéder à la bonne en écrivant explicitement `GetModuleHandleA` ou `GetModuleHandleW` et en les appelant ensuite avec des objets octets ou avec des chaînes de caractères, respectivement.

Les DLL exportent parfois des fonctions dont les noms ne sont pas des identifiants Python valides, comme `"??2@YAPAXI@Z"`. Dans ce cas, il faut utiliser `getattr()` pour accéder à la fonction :

    >>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")
    <_FuncPtr object at 0x...>
    >>>

Sous Windows, certaines DLL exportent des fonctions à travers un indice plutôt qu'à travers un nom. On accède à une fonction en indiçant l'objet DLL avec son index :

    >>> cdll.kernel32[1]
    <_FuncPtr object at 0x...>
    >>> cdll.kernel32[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "ctypes.py", line 310, in __getitem__
        func = _StdcallFuncPtr(name, self)
    AttributeError: function ordinal 0 not found
    >>>

### Appel de fonctions

You can call these functions like any other Python callable. This example uses the `rand()` function, which takes no arguments and returns a pseudo-random integer:

    >>> print(libc.rand())
    1804289383

On Windows, you can call the `GetModuleHandleA()` function, which returns a win32 module handle (passing `None` as single argument to call it with a `NULL` pointer):

    >>> print(hex(windll.kernel32.GetModuleHandleA(None)))
    0x1d000000
    >>>

Une `ValueError` est levée quand on appelle une fonction `stdcall` avec la convention d'appel `cdecl` et vice-versa :

    >>> cdll.kernel32.GetModuleHandleA(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: Procedure probably called with not enough arguments (4 bytes missing)
    >>>

    >>> windll.msvcrt.printf(b"spam")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: Procedure probably called with too many arguments (4 bytes in excess)
    >>>

Pour déterminer la convention d'appel, il faut consulter l'en-tête C ou la documentation de la fonction à appeler.

On Windows, `ctypes` uses win32 structured exception handling to prevent crashes from general protection faults when functions are called with invalid argument values:

    >>> windll.kernel32.GetModuleHandleA(32)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: exception: access violation reading 0x00000020
    >>>

There are, however, enough ways to crash Python with `ctypes`, so you should be careful anyway. The `faulthandler` module can be helpful in debugging crashes (e.g. from segmentation faults produced by erroneous C library calls).

`None`, integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls. `None` is passed as a C `NULL` pointer, bytes objects and strings are passed as pointer to the memory block that contains their data (char* or wchar_t*). Python integers are passed as the platform's default C int type, their value is masked to fit into the C type.

Before we move on calling functions with other parameter types, we have to learn more about `ctypes` data types.

### Types de données de base

`ctypes` defines a number of primitive C compatible data types:

Types de ctypes | Type C | Type Python | `_type_`
--- | --- | --- | ---
`c_bool` | _Bool | `bool` | `'?'`
`c_char` | char | 1-character `bytes` | `'c'`
`c_wchar` | `wchar_t` | 1-character `str` | `'u'`
`c_byte` | char | `int` | `'b'`
`c_ubyte` | unsigned char | `int` | `'B'`
`c_short` | short | `int` | `'h'`
`c_ushort` | unsigned short | `int` | `'H'`
`c_int` | int | `int` | `'i'` *
`c_int8` | `int8_t` | `int` | *
`c_int16` | `int16_t` | `int` | *
`c_int32` | `int32_t` | `int` | *
`c_int64` | `int64_t` | `int` | *
`c_uint` | unsigned int | `int` | `'I'` *
`c_uint8` | `uint8_t` | `int` | *
`c_uint16` | `uint16_t` | `int` | *
`c_uint32` | `uint32_t` | `int` | *
`c_uint64` | `uint64_t` | `int` | *
`c_long` | long | `int` | `'l'`
`c_ulong` | unsigned long | `int` | `'L'`
`c_longlong` | long long | `int` | `'q'` *
`c_ulonglong` | unsigned long long | `int` | `'Q'` *
`c_size_t` | `size_t` | `int` | *
`c_ssize_t` | `Py_ssize_t` | `int` | *
`c_time_t` | `time_t` | `int` | *
`c_float` | float | `float` | `'f'`
`c_double` | double | `float` | `'d'`
`c_longdouble` | long double | `float` | `'g'` *
`c_char_p` | char* (NUL terminated) | `bytes` or `None` | `'z'`
`c_wchar_p` | wchar_t* (NUL terminated) | `str` or `None` | `'Z'`
`c_void_p` | void* | `int` or `None` | `'P'`
`py_object` | PyObject* | `object` | `'O'`
VARIANT_BOOL | short int | `bool` | `'v'`

Additionally, if IEC 60559 compatible complex arithmetic (Annex G) is supported in both C and `libffi`, the following complex types are available:

Types de ctypes | Type C | Type Python | `_type_`
--- | --- | --- | ---
`c_float_complex` | float complex | `complex` | `'F'`
`c_double_complex` | double complex | `complex` | `'D'`
`c_longdouble_complex` | long double complex | `complex` | `'G'`

Il est possible de créer chacun de ces types en les appelant avec une valeur d'initialisation du bon type et avec une valeur cohérente :

    >>> c_int()
    c_long(0)
    >>> c_wchar_p("Hello, World")
    c_wchar_p(140018365411392)
    >>> c_ushort(-3)
    c_ushort(65533)
    >>>

The constructors for numeric types will convert input using `__bool__()`, `__index__()` (for `int`), `__float__()` or `__complex__()`. This means `c_bool` accepts any object with a truth value:

    >>> empty_list = []
    >>> c_bool(empty_list)
    c_bool(False)

Ces types étant des mutables, leur valeur peut aussi être modifiée après coup :

    >>> i = c_int(42)
    >>> print(i)
    c_long(42)
    >>> print(i.value)
    42
    >>> i.value = -99
    >>> print(i.value)
    -99
    >>>

Assigning a new value to instances of the pointer types `c_char_p`, `c_wchar_p`, and `c_void_p` changes the memory location they point to, not the contents of the memory block (of course not, because Python string objects are immutable):

    >>> s = "Hello, World"
    >>> c_s = c_wchar_p(s)
    >>> print(c_s)
    c_wchar_p(139966785747344)
    >>> print(c_s.value)
    Hello World
    >>> c_s.value = "Hi, there"
    >>> print(c_s)              # the memory location has changed
    c_wchar_p(139966783348904)
    >>> print(c_s.value)
    Hi, there
    >>> print(s)                # first object is unchanged
    Hello, World
    >>>

Cependant, prenez garde à ne pas en passer à des fonctions qui prennent en paramètre des pointeurs sur de la mémoire modifiable. S'il vous faut de la mémoire modifiable, ctypes fournit la fonction `create_string_buffer()` qui en crée de plusieurs façons. L'attribut `raw` permet d'accéder à (ou de modifier) un bloc mémoire ; l'attribut `value` permet d'y accéder comme à une chaîne de caractères terminée par NUL :

    >>> from ctypes import *
    >>> p = create_string_buffer(3)            # create a 3 byte buffer, initialized to NUL bytes
    >>> print(sizeof(p), repr(p.raw))
    3 b'\x00\x00\x00'
    >>> p = create_string_buffer(b"Hello")     # create a buffer containing a NUL terminated string
    >>> print(sizeof(p), repr(p.raw))
    6 b'Hello\x00'
    >>> print(repr(p.value))
    b'Hello'
    >>> p = create_string_buffer(b"Hello", 10) # create a 10 byte buffer
    >>> print(sizeof(p), repr(p.raw))
    10 b'Hello\x00\x00\x00\x00\x00'
    >>> p.value = b"Hi"
    >>> print(sizeof(p), repr(p.raw))
    10 b'Hi\x00lo\x00\x00\x00\x00\x00'
    >>>

The `create_string_buffer()` function replaces the old `c_buffer()` function (which is still available as an alias). To create a mutable memory block containing unicode characters of the C type `wchar_t`, use the `create_unicode_buffer()` function.

### Appel de fonctions, suite

printf utilise la vraie sortie standard, et non `sys.stdout` ; les exemples suivants ne fonctionnent donc que dans une invite de commande et non depuis IDLE or PythonWin :

    >>> printf = libc.printf
    >>> printf(b"Hello, %s\n", b"World!")
    Hello, World!
    14
    >>> printf(b"Hello, %S\n", "World!")
    Hello, World!
    14
    >>> printf(b"%d bottles of beer\n", 42)
    42 bottles of beer
    19
    >>> printf(b"%f bottles of beer\n", 42.5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ctypes.ArgumentError: argument 2: TypeError: Don't know how to convert parameter 2
    >>>

As has been mentioned before, all Python types except integers, strings, and bytes objects have to be wrapped in their corresponding `ctypes` type, so that they can be converted to the required C data type:

    >>> printf(b"An int %d, a double %f\n", 1234, c_double(3.14))
    An int 1234, a double 3.140000
    31
    >>>

### Calling variadic functions

On a lot of platforms calling variadic functions through ctypes is exactly the same as calling functions with a fixed number of parameters. On some platforms, and in particular ARM64 for Apple Platforms, the calling convention for variadic functions is different than that for regular functions.

On those platforms it is required to specify the `argtypes` attribute for the regular, non-variadic, function arguments:

    libc.printf.argtypes = [ctypes.c_char_p]

Because specifying the attribute does not inhibit portability it is advised to always specify `argtypes` for all variadic functions.

### Appel de fonctions avec des types de données personnalisés

You can also customize `ctypes` argument conversion to allow instances of your own classes be used as function arguments. `ctypes` looks for an `_as_parameter_` attribute and uses this as the function argument. The attribute must be an integer, string, bytes, a `ctypes` instance, or an object with an `_as_parameter_` attribute:

    >>> class Bottles:
    ...     def __init__(self, number):
    ...         self._as_parameter_ = number
    ...
    >>> bottles = Bottles(42)
    >>> printf(b"%d bottles of beer\n", bottles)
    42 bottles of beer
    19
    >>>

If you don't want to store the instance's data in the `_as_parameter_` instance variable, you could define a `property` which makes the attribute available on request.

### Définition du type des arguments nécessaires (prototypes de fonction)

It is possible to specify the required argument types of functions exported from DLLs by setting the `argtypes` attribute.

`argtypes` must be a sequence of C data types (the `printf()` function is probably not a good example here, because it takes a variable number and different types of parameters depending on the format string, on the other hand this is quite handy to experiment with this feature):

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

Définir un format empêche de passer des arguments de type incompatible (comme le fait le prototype d'une fonction C) et tente de convertir les arguments en des types valides :

    >>> printf(b"%d %d %d", 1, 2, 3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ctypes.ArgumentError: argument 2: TypeError: 'int' object cannot be interpreted as ctypes.c_char_p
    >>> printf(b"%s %d %f\n", b"X", 2, 3)
    X 2 3.000000
    13
    >>>

If you have defined your own classes which you pass to function calls, you have to implement a `from_param()` class method for them to be able to use them in the `argtypes` sequence. The `from_param()` class method receives the Python object passed to the function call, it should do a typecheck or whatever is needed to make sure this object is acceptable, and then return the object itself, its `_as_parameter_` attribute, or whatever you want to pass as the C function argument in this case. Again, the result should be an integer, string, bytes, a `ctypes` instance, or an object with an `_as_parameter_` attribute.

### Types de sortie

By default functions are assumed to return the C int type. Other return types can be specified by setting the `restype` attribute of the function object.

The C prototype of `time()` is `time_t time(time_t *)`. Because `time_t` might be of a different type than the default return type int, you should specify the `restype` attribute:

    >>> libc.time.restype = c_time_t

The argument types can be specified using `argtypes`:

    >>> libc.time.argtypes = (POINTER(c_time_t),)

To call the function with a `NULL` pointer as first argument, use `None`:

    >>> print(libc.time(None))
    1150640792

Here is a more advanced example, it uses the `strchr()` function, which expects a string pointer and a char, and returns a pointer to a string:

    >>> strchr = libc.strchr
    >>> strchr(b"abcdef", ord("d"))
    8059983
    >>> strchr.restype = c_char_p    # c_char_p is a pointer to a string
    >>> strchr(b"abcdef", ord("d"))
    b'def'
    >>> print(strchr(b"abcdef", ord("x")))
    None
    >>>

If you want to avoid the `ord("x")` calls above, you can set the `argtypes` attribute, and the second argument will be converted from a single character Python bytes object into a C char:

    >>> strchr.restype = c_char_p
    >>> strchr.argtypes = [c_char_p, c_char]
    >>> strchr(b"abcdef", b"d")
    b'def'
    >>> strchr(b"abcdef", b"def")
    Traceback (most recent call last):
    ctypes.ArgumentError: argument 2: TypeError: one character bytes, bytearray or integer expected
    >>> print(strchr(b"abcdef", b"x"))
    None
    >>> strchr(b"abcdef", b"d")
    b'def'
    >>>

You can also use a callable Python object (a function or a class for example) as the `restype` attribute, if the foreign function returns an integer. The callable will be called with the integer the C function returns, and the result of this call will be used as the result of your function call. This is useful to check for error return values and automatically raise an exception:

    >>> GetModuleHandle = windll.kernel32.GetModuleHandleA
    >>> def ValidHandle(value):
    ...     if value == 0:
    ...         raise WinError()
    ...     return value
    ...
    >>>
    >>> GetModuleHandle.restype = ValidHandle
    >>> GetModuleHandle(None)
    486539264
    >>> GetModuleHandle("something silly")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in ValidHandle
    OSError: [Errno 126] The specified module could not be found.
    >>>

`WinError` appelle l'API Windows `FormatMessage()` pour obtenir une représentation de la chaîne de caractères qui correspond au code d'erreur, et renvoie une exception. `WinError` prend en paramètre — optionnel — le code d'erreur. Si celui-ci n'est pas passé, elle appelle `GetLastError()` pour le récupérer.

Please note that a much more powerful error checking mechanism is available through the `errcheck` attribute; see the reference manual for details.

### Passage de pointeurs (passage de paramètres par référence)

Il arrive qu'une fonction C du code à interfacer requière un pointeur vers un certain type de donnée en paramètre, typiquement pour écrire à l'endroit correspondant ou si la donnée est trop grande pour pouvoir être passée par valeur. Ce mécanisme est appelé passage de paramètres par référence.

`ctypes` exports the `byref()` function which is used to pass parameters by reference. The same effect can be achieved with the `pointer()` function, although `pointer()` does a lot more work since it constructs a real pointer object, so it is faster to use `byref()` if you don't need the pointer object in Python itself:

    >>> i = c_int()
    >>> f = c_float()
    >>> s = create_string_buffer(b'\000' * 32)
    >>> print(i.value, f.value, repr(s.value))
    0 0.0 b''
    >>> libc.sscanf(b"1 3.14 Hello", b"%d %f %s",
    ...             byref(i), byref(f), s)
    3
    >>> print(i.value, f.value, repr(s.value))
    1 3.1400001049 b'Hello'
    >>>

### Structures et unions

Structures and unions must derive from the `Structure` and `Union` base classes which are defined in the `ctypes` module. Each subclass must define a `_fields_` attribute. `_fields_` must be a list of 2-tuples, containing a field name and a field type.

The field type must be a `ctypes` type like `c_int`, or any other derived `ctypes` type: structure, union, array, pointer.

Voici un exemple simple : une structure POINT qui contient deux entiers x et y et qui montre également comment instancier une structure avec le constructeur :

    >>> from ctypes import *
    >>> class POINT(Structure):
    ...     _fields_ = [("x", c_int),
    ...                 ("y", c_int)]
    ...
    >>> point = POINT(10, 20)
    >>> print(point.x, point.y)
    10 20
    >>> point = POINT(y=5)
    >>> print(point.x, point.y)
    0 5
    >>> POINT(1, 2, 3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: too many initializers
    >>>

Il est bien entendu possible de créer des structures plus complexes. Une structure peut elle-même contenir d'autres structures en prenant une structure comme type de champ.

Voici une structure RECT qui contient deux POINTs upperleft et lowerright :

    >>> class RECT(Structure):
    ...     _fields_ = [("upperleft", POINT),
    ...                 ("lowerright", POINT)]
    ...
    >>> rc = RECT(point)
    >>> print(rc.upperleft.x, rc.upperleft.y)
    0 5
    >>> print(rc.lowerright.x, rc.lowerright.y)
    0 0
    >>>

Une structure encapsulée peut être instanciée par un constructeur de plusieurs façons :

    >>> r = RECT(POINT(1, 2), POINT(3, 4))
    >>> r = RECT((1, 2), (3, 4))

Field descriptors can be retrieved from the class, they are useful for debugging because they can provide useful information. See `CField`:

    >>> POINT.x
    <ctypes.CField 'x' type=c_int, ofs=0, size=4>
    >>> POINT.y
    <ctypes.CField 'y' type=c_int, ofs=4, size=4>
    >>>

Avertissement

`ctypes` does not support passing unions or structures with bit-fields to functions by value. While this may work on 32-bit x86, it's not guaranteed by the library to work in the general case. Unions and structures with bit-fields should always be passed to functions by pointer.

### Structure/union layout, alignment and byte order

By default, Structure and Union fields are laid out in the same way the C compiler does it. It is possible to override this behavior entirely by specifying a `_layout_` class attribute in the subclass definition; see the attribute documentation for details.

It is possible to specify the maximum alignment for the fields and/or for the structure itself by setting the class attributes `_pack_` and/or `_align_`, respectively. See the attribute documentation for details.

`ctypes` uses the native byte order for Structures and Unions. To build structures with non-native byte order, you can use one of the `BigEndianStructure`, `LittleEndianStructure`, `BigEndianUnion`, and `LittleEndianUnion` base classes. These classes cannot contain pointer fields.

### Champs de bits dans les structures et les unions

It is possible to create structures and unions containing bit fields. Bit fields are only possible for integer fields, the bit width is specified as the third item in the `_fields_` tuples:

    >>> class Int(Structure):
    ...     _fields_ = [("first_16", c_int, 16),
    ...                 ("second_16", c_int, 16)]
    ...
    >>> print(Int.first_16)
    <ctypes.CField 'first_16' type=c_int, ofs=0, bit_size=16, bit_offset=0>
    >>> print(Int.second_16)
    <ctypes.CField 'second_16' type=c_int, ofs=0, bit_size=16, bit_offset=16>

It is important to note that bit field allocation and layout in memory are not defined as a C standard; their implementation is compiler-specific. By default, Python will attempt to match the behavior of a "native" compiler for the current platform. See the `_layout_` attribute for details on the default behavior and how to change it.

### Tableaux

Les tableaux sont des séquences qui contiennent un nombre fixe d'instances du même type.

La meilleure façon de créer des tableaux consiste à multiplier le type de donnée par un entier positif :

    TenPointsArrayType = POINT * 10

Voici un exemple — un peu artificiel — d'une structure contenant, entre autres, 4 POINTs :

    >>> from ctypes import *
    >>> class POINT(Structure):
    ...     _fields_ = ("x", c_int), ("y", c_int)
    ...
    >>> class MyStruct(Structure):
    ...     _fields_ = [("a", c_int),
    ...                 ("b", c_float),
    ...                 ("point_array", POINT * 4)]
    >>>
    >>> print(len(MyStruct().point_array))
    4
    >>>

Comme d'habitude, on crée les instances en appelant la classe :

    arr = TenPointsArrayType()
    for pt in arr:
        print(pt.x, pt.y)

Le code précédent affiche une suite de `0 0` car le contenu du tableau est initialisé avec des zéros.

Des valeurs d'initialisation du bon type peuvent être passées :

    >>> from ctypes import *
    >>> TenIntegers = c_int * 10
    >>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    >>> print(ii)
    <c_long_Array_10 object at 0x...>
    >>> for i in ii: print(i, end=" ")
    ...
    1 2 3 4 5 6 7 8 9 10
    >>>

### Pointeurs

Pointer instances are created by calling the `pointer()` function on a `ctypes` type:

    >>> from ctypes import *
    >>> i = c_int(42)
    >>> pi = pointer(i)
    >>>

Les instances de pointeurs ont un attribut `contents` qui renvoie l'objet pointé (l'objet `i` ci-dessus) :

    >>> pi.contents
    c_long(42)
    >>>

Note that `ctypes` does not have OOR (original object return), it constructs a new, equivalent object each time you retrieve an attribute:

    >>> pi.contents is i
    False
    >>> pi.contents is pi.contents
    False
    >>>

Affecter une autre instance de `c_int` à l'attribut contents du pointeur fait pointer le pointeur vers l'adresse mémoire de cette nouvelle instance :

    >>> i = c_int(99)
    >>> pi.contents = i
    >>> pi.contents
    c_long(99)
    >>>

Il est possible d'indexer les pointeurs par des entiers :

    >>> pi[0]
    99
    >>>

Affecter à travers un indice change la valeur pointée :

    >>> print(i)
    c_long(99)
    >>> pi[0] = 22
    >>> print(i)
    c_long(22)
    >>>

Si vous êtes sûr de vous, vous pouvez utiliser d'autres valeurs que 0, comme en C : il est ainsi possible de modifier une zone mémoire de votre choix. De manière générale cette fonctionnalité ne s'utilise que sur un pointeur renvoyé par une fonction C, pointeur que vous savez pointer vers un tableau et non sur un seul élément.

Behind the scenes, the `pointer()` function does more than simply create pointer instances, it has to create pointer types first. This is done with the `POINTER()` function, which accepts any `ctypes` type, and returns a new type:

    >>> PI = POINTER(c_int)
    >>> PI
    <class 'ctypes.LP_c_long'>
    >>> PI(42)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected c_long instead of int
    >>> PI(c_int(42))
    <ctypes.LP_c_long object at 0x...>
    >>>

Appeler le pointeur sur type sans arguments crée un pointeur `NULL`. Les pointeurs `NULL` s'évaluent à `False` :

    >>> null_ptr = POINTER(c_int)()
    >>> print(bool(null_ptr))
    False
    >>>

`ctypes` checks for `NULL` when dereferencing pointers (but dereferencing invalid non-`NULL` pointers would crash Python):

    >>> null_ptr[0]
    Traceback (most recent call last):
        ....
    ValueError: NULL pointer access
    >>>

    >>> null_ptr[0] = 1234
    Traceback (most recent call last):
        ....
    ValueError: NULL pointer access
    >>>

### Thread safety without the GIL

From Python 3.13 onward, the GIL can be disabled on the free-threaded build. In ctypes, reads and writes to a single object concurrently is safe, but not across multiple objects:

>     >>> number = c_int(42)
>     >>> pointer_a = pointer(number)
>     >>> pointer_b = pointer(number)

In the above, it's only safe for one object to read and write to the address at once if the GIL is disabled. So, `pointer_a` can be shared and written to across multiple threads, but only if `pointer_b` is not also attempting to do the same. If this is an issue, consider using a `threading.Lock` to synchronize access to memory:

>     >>> import threading
>     >>> lock = threading.Lock()
>     >>> # Thread 1
>     >>> with lock:
>     ...    pointer_a.contents = 24
>     >>> # Thread 2
>     >>> with lock:
>     ...    pointer_b.contents = 42

### Conversions de type

Usually, ctypes does strict type checking. This means, if you have `POINTER(c_int)` in the `argtypes` list of a function or as the type of a member field in a structure definition, only instances of exactly the same type are accepted. There are some exceptions to this rule, where ctypes accepts other objects. For example, you can pass compatible array instances instead of pointer types. So, for `POINTER(c_int)`, ctypes accepts an array of c_int:

    >>> class Bar(Structure):
    ...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]
    ...
    >>> bar = Bar()
    >>> bar.values = (c_int * 3)(1, 2, 3)
    >>> bar.count = 3
    >>> for i in range(bar.count):
    ...     print(bar.values[i])
    ...
    1
    2
    3
    >>>

In addition, if a function argument is explicitly declared to be a pointer type (such as `POINTER(c_int)`) in `argtypes`, an object of the pointed type (`c_int` in this case) can be passed to the function. ctypes will apply the required `byref()` conversion in this case automatically.

Pour mettre un champ de type POINTER à `NULL`, il faut lui affecter `None` :

    >>> bar.values = None
    >>>

Sometimes you have instances of incompatible types. In C, you can cast one type into another type. `ctypes` provides a `cast()` function which can be used in the same way. The `Bar` structure defined above accepts `POINTER(c_int)` pointers or `c_int` arrays for its `values` field, but not instances of other types:

    >>> bar.values = (c_byte * 4)()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance
    >>>

C'est là que la fonction `cast()` intervient.

La fonction `cast()` permet de convertir une instance de ctypes en un pointeur vers un type de données ctypes différent. `cast()` prend deux paramètres : un objet ctypes qui est, ou qui peut être converti en, un certain pointeur et un type pointeur de ctypes. Elle renvoie une instance du second argument, qui pointe sur le même bloc mémoire que le premier argument :

    >>> a = (c_byte * 4)()
    >>> cast(a, POINTER(c_int))
    <ctypes.LP_c_long object at ...>
    >>>

Ainsi, la fonction `cast()` permet de remplir le champ `values` de la structure `Bar` :

    >>> bar = Bar()
    >>> bar.values = cast((c_byte * 4)(), POINTER(c_int))
    >>> print(bar.values[0])
    0
    >>>

### Types incomplets

Un type incomplet est une structure, une union ou un tableau dont les membres ne sont pas encore définis. C'est l'équivalent d'une déclaration avancée en C, où la définition est fournie plus tard :

    struct cell; /* forward declaration */

    struct cell {
        char *name;
        struct cell *next;
    };

Une traduction naïve, mais invalide, en code ctypes ressemblerait à ça :

    >>> class cell(Structure):
    ...     _fields_ = [("name", c_char_p),
    ...                 ("next", POINTER(cell))]
    ...
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in cell
    NameError: name 'cell' is not defined
    >>>

because the new `class cell` is not available in the class statement itself. In `ctypes`, we can define the `cell` class and set the `_fields_` attribute later, after the class statement:

    >>> from ctypes import *
    >>> class cell(Structure):
    ...     pass
    ...
    >>> cell._fields_ = [("name", c_char_p),
    ...                  ("next", POINTER(cell))]
    >>>

Essayons. Nous créons deux instances de `cell`, les faisons pointer l'une sur l'autre et enfin nous suivons quelques maillons de la chaîne de pointeurs :

    >>> c1 = cell()
    >>> c1.name = b"foo"
    >>> c2 = cell()
    >>> c2.name = b"bar"
    >>> c1.next = pointer(c2)
    >>> c2.next = pointer(c1)
    >>> p = c1
    >>> for i in range(8):
    ...     print(p.name, end=" ")
    ...     p = p.next[0]
    ...
    foo bar foo bar foo bar foo bar
    >>>

### Fonctions de rappel

`ctypes` allows creating C callable function pointers from Python callables. These are sometimes called callback functions.

Tout d'abord, il faut créer une classe pour la fonction de rappel. La classe connaît la convention d'appel, le type de retour ainsi que le nombre et le type de paramètres que la fonction accepte.

La fabrique `CFUNCTYPE()` crée un type pour les fonctions de rappel qui suivent la convention d'appel `cdecl`. En Windows, c'est la fabrique `WINFUNCTYPE()` qui crée un type pour les fonctions de rappel qui suivent la convention d'appel `stdcall`.

Le premier paramètre de ces deux fonctions est le type de retour, et les suivants sont les types des arguments qu'attend la fonction de rappel.

I will present an example here which uses the standard C library's `qsort()` function, that is used to sort items with the help of a callback function. `qsort()` will be used to sort an array of integers:

    >>> IntArray5 = c_int * 5
    >>> ia = IntArray5(5, 1, 7, 33, 99)
    >>> qsort = libc.qsort
    >>> qsort.restype = None
    >>>

`qsort()` must be called with a pointer to the data to sort, the number of items in the data array, the size of one item, and a pointer to the comparison function, the callback. The callback will then be called with two pointers to items, and it must return a negative integer if the first item is smaller than the second, a zero if they are equal, and a positive integer otherwise.

Ainsi notre fonction de rappel reçoit des pointeurs vers des entiers et doit renvoyer un entier. Créons d'abord le `type` pour la fonction de rappel :

    >>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
    >>>

Pour commencer, voici une fonction de rappel simple qui affiche les valeurs qu'on lui passe :

    >>> def py_cmp_func(a, b):
    ...     print("py_cmp_func", a[0], b[0])
    ...     return 0
    ...
    >>> cmp_func = CMPFUNC(py_cmp_func)
    >>>

Résultat :

    >>> qsort(ia, len(ia), sizeof(c_int), cmp_func)
    py_cmp_func 5 1
    py_cmp_func 33 99
    py_cmp_func 7 33
    py_cmp_func 5 7
    py_cmp_func 1 7
    >>>

À présent, comparons pour de vrai les deux entiers et renvoyons un résultat utile :

    >>> def py_cmp_func(a, b):
    ...     print("py_cmp_func", a[0], b[0])
    ...     return a[0] - b[0]
    ...
    >>>
    >>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func))
    py_cmp_func 5 1
    py_cmp_func 33 99
    py_cmp_func 7 33
    py_cmp_func 1 7
    py_cmp_func 5 7
    >>>

Et comme il est facile de le voir, notre tableau est désormais classé :

    >>> for i in ia: print(i, end=" ")
    ...
    1 5 7 33 99
    >>>

Ces fonctions peuvent aussi être utilisées comme des décorateurs ; il est donc possible d'écrire :

    >>> @CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
    ... def py_cmp_func(a, b):
    ...     print("py_cmp_func", a[0], b[0])
    ...     return a[0] - b[0]
    ...
    >>> qsort(ia, len(ia), sizeof(c_int), py_cmp_func)
    py_cmp_func 5 1
    py_cmp_func 33 99
    py_cmp_func 7 33
    py_cmp_func 1 7
    py_cmp_func 5 7
    >>>

Note

Make sure you keep references to `CFUNCTYPE()` objects as long as they are used from C code. `ctypes` doesn't, and if you don't, they may be garbage collected, crashing your program when a callback is made.

Notez aussi que si la fonction de rappel est appelée dans un fil d'exécution créé hors de Python (p. ex. par du code externe qui appelle la fonction de rappel), ctypes crée un nouveau fil Python « creux » à chaque fois. Ce comportement est acceptable pour la plupart des cas d'utilisation, mais cela implique que les valeurs stockées avec `threading.local` ne seront pas persistantes d'un appel à l'autre, même si les appels proviennent du même fil d'exécution C.

### Accès aux variables exportées depuis une DLL

Some shared libraries not only export functions, they also export variables. An example in the Python library itself is the `Py_Version`, Python runtime version number encoded in a single constant integer.

`ctypes` can access values like this with the `in_dll()` class methods of the type. pythonapi is a predefined symbol giving access to the Python C api:

    >>> version = ctypes.c_int.in_dll(ctypes.pythonapi, "Py_Version")
    >>> print(hex(version.value))
    0x30c00a0

Le pointeur `PyImport_FrozenModules` exposé par Python est un autre exemple complet de l'utilisation de pointeurs.

Citons la documentation :

> This pointer is initialized to point to an array of `_frozen` records, terminated by one whose members are all `NULL` or zero. When a frozen module is imported, it is searched in this table. Third-party code could play tricks with this to provide a dynamically created collection of frozen modules.

So manipulating this pointer could even prove useful. To restrict the example size, we show only how this table can be read with `ctypes`:

    >>> from ctypes import *
    >>>
    >>> class struct_frozen(Structure):
    ...     _fields_ = [("name", c_char_p),
    ...                 ("code", POINTER(c_ubyte)),
    ...                 ("size", c_int),
    ...                 ("get_code", POINTER(c_ubyte)),  # Function pointer
    ...                ]
    ...
    >>>

We have defined the `_frozen` data type, so we can get the pointer to the table:

    >>> FrozenTable = POINTER(struct_frozen)
    >>> table = FrozenTable.in_dll(pythonapi, "_PyImport_FrozenBootstrap")
    >>>

Puisque `table` est un `pointer` vers un tableau d'entrées de `struct_frozen`, il est possible d'itérer dessus, mais il faut être certain que la boucle se termine, car les pointeurs n'ont pas de taille. Tôt ou tard, il planterait probablement avec une erreur de segmentation ou autre, donc mieux vaut sortir de la boucle quand on lit l'entrée `NULL` :

    >>> for item in table:
    ...     if item.name is None:
    ...         break
    ...     print(item.name.decode("ascii"), item.size)
    ...
    _frozen_importlib 31764
    _frozen_importlib_external 41499
    zipimport 12345
    >>>

Le fait que le standard Python possède un module et un paquet figés (indiqués par la valeur négative du membre size) est peu connu, cela ne sert qu'aux tests. Essayez avec `import __hello__` par exemple.

### Pièges

There are some edges in `ctypes` where you might expect something other than what actually happens.

Examinons l'exemple suivant :

    >>> from ctypes import *
    >>> class POINT(Structure):
    ...     _fields_ = ("x", c_int), ("y", c_int)
    ...
    >>> class RECT(Structure):
    ...     _fields_ = ("a", POINT), ("b", POINT)
    ...
    >>> p1 = POINT(1, 2)
    >>> p2 = POINT(3, 4)
    >>> rc = RECT(p1, p2)
    >>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
    1 2 3 4
    >>> # now swap the two points
    >>> rc.a, rc.b = rc.b, rc.a
    >>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
    3 4 3 4
    >>>

Diantre. On s'attendait certainement à ce que le dernier résultat affiche `3 4 1 2`. Que s'est-il passé ? Les étapes de la ligne `rc.a, rc.b = rc.b, rc.a` ci-dessus sont les suivantes :

    >>> temp0, temp1 = rc.b, rc.a
    >>> rc.a = temp0
    >>> rc.b = temp1
    >>>

Les objets `temp0` et `temp1` utilisent encore le tampon interne de l'objet `rc` ci-dessus. Donc exécuter `rc.a = temp0` copie le contenu du tampon de `temp0` dans celui de `rc`. Ce qui, par ricochet, modifie le contenu de `temp1`. Et donc, la dernière affectation, `rc.b = temp1`, n'a pas l'effet escompté.

Gardez en tête qu'accéder au sous-objet depuis une Structure, une Union ou un Array ne copie pas le sous-objet, mais crée un objet interface qui accède au tampon sous-jacent de l'objet initial.

Un autre exemple de comportement a priori inattendu est le suivant :

    >>> s = c_char_p()
    >>> s.value = b"abc def ghi"
    >>> s.value
    b'abc def ghi'
    >>> s.value is s.value
    False
    >>>

Note

La valeur d'une instance de `c_char_p` ne peut être initialisée qu'avec un octet ou un entier.

Pourquoi cela affiche-t'il `False` ? Les instances ctypes sont des objets qui contiennent un bloc mémoire et des descriptor qui donnent accès au contenu du ce bloc. Stocker un objet Python dans le bloc mémoire ne stocke pas l'objet même ; seuls ses `contents` le sont. Accéder au `contents` crée un nouvel objet Python à chaque fois !

### Types de données à taille flottante

`ctypes` provides some support for variable-sized arrays and structures.

La fonction `resize()` permet de redimensionner la taille du tampon mémoire d'un objet ctypes existant. Cette fonction prend l'objet comme premier argument et la taille en octets désirée comme second. La taille du tampon mémoire ne peut pas être inférieure à celle occupée par un objet unitaire du type considéré. Une `ValueError` est levée si c'est le cas :

    >>> short_array = (c_short * 4)()
    >>> print(sizeof(short_array))
    8
    >>> resize(short_array, 4)
    Traceback (most recent call last):
        ...
    ValueError: minimum size is 8
    >>> resize(short_array, 32)
    >>> sizeof(short_array)
    32
    >>> sizeof(type(short_array))
    8
    >>>

Cela dit, comment accéder aux éléments supplémentaires contenus dans le tableau ? Vu que le type ne connaît que 4 éléments, on obtient une erreur si l'on accède aux suivants :

    >>> short_array[:]
    [0, 0, 0, 0]
    >>> short_array[7]
    Traceback (most recent call last):
        ...
    IndexError: invalid index
    >>>

Another way to use variable-sized data types with `ctypes` is to use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by case basis.

## Référence du module

### Recherche de bibliothèques partagées

Les langages compilés ont besoin d'accéder aux bibliothèques partagées au moment de la compilation, de l'édition de liens et pendant l'exécution du programme.

The purpose of the `find_library()` function is to locate a library in a way similar to what the compiler or runtime loader does (on platforms with several versions of a shared library the most recent should be loaded), while the ctypes library loaders act like when a program is run, and call the runtime loader directly.

The `ctypes.util` module provides a function which can help to determine the library to load.

ctypes.util.find_library(name)

  Tente de trouver une bibliothèque et en renvoie le chemin. name est le nom de la bibliothèque sans préfixe — comme lib — ni suffixe — comme `.so`, `.dylib` ou un numéro de version (c.-à-d. la même forme que l'option POSIX de l'éditeur de lien `-l`). Si la fonction ne parvient pas à trouver de bibliothèque, elle renvoie `None`.

Le mode opératoire exact dépend du système.

On Linux, `find_library()` tries to run external programs (`/sbin/ldconfig`, `gcc`, `objdump` and `ld`) to find the library file. It returns the filename of the library file.

Note that if the output of these programs does not correspond to the dynamic linker used by Python, the result of this function may be misleading.

Modifié dans la version 3.6: Sous Linux, si les autres moyens échouent, la fonction utilise la variable d'environnement `LD_LIBRARY_PATH` pour trouver la bibliothèque.

Voici quelques exemples :

    >>> from ctypes.util import find_library
    >>> find_library("m")
    'libm.so.6'
    >>> find_library("c")
    'libc.so.6'
    >>> find_library("bz2")
    'libbz2.so.1.0'
    >>>

On macOS and Android, `find_library()` uses the system's standard naming schemes and paths to locate the library, and returns a full pathname if successful:

    >>> from ctypes.util import find_library
    >>> find_library("c")
    '/usr/lib/libc.dylib'
    >>> find_library("m")
    '/usr/lib/libm.dylib'
    >>> find_library("bz2")
    '/usr/lib/libbz2.dylib'
    >>> find_library("AGL")
    '/System/Library/Frameworks/AGL.framework/AGL'
    >>>

On Windows, `find_library()` searches along the system search path, and returns the full pathname, but since there is no predefined naming scheme a call like `find_library("c")` will fail and return `None`.

If wrapping a shared library with `ctypes`, it may be better to determine the shared library name at development time, and hardcode that into the wrapper module instead of using `find_library()` to locate the library at runtime.

### Listing loaded shared libraries

When writing code that relies on code loaded from shared libraries, it can be useful to know which shared libraries have already been loaded into the current process.

The `ctypes.util` module provides the `dllist()` function, which calls the different APIs provided by the various platforms to help determine which shared libraries have already been loaded into the current process.

The exact output of this function will be system dependent. On most platforms, the first entry of this list represents the current process itself, which may be an empty string. For example, on glibc-based Linux, the return may look like:

    >>> from ctypes.util import dllist
    >>> dllist()
    ['', 'linux-vdso.so.1', '/lib/x86_64-linux-gnu/libm.so.6', '/lib/x86_64-linux-gnu/libc.so.6', ... ]

### Chargement des bibliothèques partagées

There are several ways to load shared libraries into the Python process. One way is to instantiate `CDLL` or one of its subclasses:

class ctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

  Represents a loaded shared library.

  Functions in this library use the standard C calling convention, and are assumed to return int. The Python global interpreter lock is released before calling any function exported by these libraries, and reacquired afterwards. For different function behavior, use a subclass: `OleDLL`, `WinDLL`, or `PyDLL`.

  If you have an existing `handle` to an already loaded shared library, it can be passed as the handle argument to wrap the opened library in a new `CDLL` object. In this case, name is only used to set the `_name` attribute, but it may be adjusted and/or validated.

  If handle is `None`, the underlying platform's dlopen(3) or `LoadLibrary()` function is used to load the library into the process, and to get a handle to it.

  name is the pathname of the shared library to open. If name does not contain a path separator, the library is found in a platform-specific way.

  On non-Windows systems, name can be `None`. In this case, `dlopen()` is called with `NULL`, which opens the main program as a "library". (Some systems do the same is name is empty; `None`/`NULL` is more portable.)

  CPython implementation detail

  Since CPython is linked to `libc`, a `None` name is often used to access the C standard library:

      >>> printf = ctypes.CDLL(None).printf
      >>> printf.argtypes = [ctypes.c_char_p]
      >>> printf(b"hello\n")
      hello
      6

  To access the Python C API, prefer `ctypes.pythonapi` which works across platforms.

  Le mode de chargement de la bibliothèque est défini par le paramètre mode. Pour plus de détails, référez-vous à l'entrée dlopen(3) du manuel. En Windows, mode est ignoré. Sur les systèmes POSIX, RTLD_NOW y est toujours ajouté. Ceci n'est pas configurable.

  The use_errno parameter, when set to true, enables a ctypes mechanism that allows accessing the system `errno` error number in a safe way. `ctypes` maintains a thread-local copy of the system's `errno` variable; if you call foreign functions created with `use_errno=True` then the `errno` value before the function call is swapped with the ctypes private copy, the same happens immediately after the function call.

  La fonction `ctypes.get_errno()` renvoie la valeur de la copie privée de ctypes. La fonction `ctypes.set_errno()` affecte une nouvelle valeur à la copie privée et renvoie l'ancienne valeur.

  The use_last_error parameter, when set to true, enables the same mechanism for the Windows error code which is managed by the `GetLastError()` and `SetLastError()` Windows API functions; `ctypes.get_last_error()` and `ctypes.set_last_error()` are used to request and change the ctypes private copy of the windows error code.

  The winmode parameter is used on Windows to specify how the library is loaded (since mode is ignored). It takes any value that is valid for the Win32 API `LoadLibraryEx` flags parameter. When omitted, the default is to use the flags that result in the most secure DLL load, which avoids issues such as DLL hijacking. Passing the full path to the DLL is the safest way to ensure the correct library and dependencies are loaded.

  En Windows, créer une instance de `CDLL` peut échouer, même si une DLL du bon nom existe. Quand une des dépendances de la DLL à charger ne peut pas être trouvée, une `OSError` est levée avec le message "[WinError 126] The specified module could not be found". Ce message d'erreur ne contient pas le nom de la DLL manquante car l'API Windows ne fournit pas cette information. Cela rend l'erreur délicate à analyser ; pour la résoudre, il faut lister toutes les dépendances de la DLL et trouver celle qui manque en utilisant des outils de débogage et de traçage Windows.

  Voir aussi

  Microsoft DUMPBIN tool -- A tool to find DLL dependents.

  Modifié dans la version 3.8: Ajout du paramètre winmode.

  Modifié dans la version 3.12: The name parameter can now be a path-like object.

  Instances of this class have no public methods. Functions exported by the shared library can be accessed as attributes or by index. Please note that accessing the function through an attribute caches the result and therefore accessing it repeatedly returns the same object each time. On the other hand, accessing it through an index returns a new object each time:

      >>> from ctypes import CDLL
      >>> libc = CDLL("libc.so.6")  # On Linux
      >>> libc.time == libc.time
      True
      >>> libc['time'] == libc['time']
      False

  The following public attributes are available. Their name starts with an underscore to not clash with exported function names:

  _handle

    Le lien système d'accès à la bibliothèque.

  _name

    Nom de la bibliothèque donné au constructeur.

class ctypes.OleDLL

  See `CDLL`, the superclass, for common information.

  Functions in this library use the `stdcall` calling convention, and are assumed to return the windows specific `HRESULT` code. `HRESULT` values contain information specifying whether the function call failed or succeeded, together with additional error code. If the return value signals a failure, an `OSError` is automatically raised.

  Disponibilité: Windows

  Modifié dans la version 3.3: `WindowsError` used to be raised, which is now an alias of `OSError`.

class ctypes.WinDLL

  See `CDLL`, the superclass, for common information.

  Functions in these libraries use the `stdcall` calling convention, and are assumed to return int by default.

  Disponibilité: Windows

class ctypes.PyDLL

  See `CDLL`, the superclass, for common information.

  When functions in this library are called, the Python GIL is not released during the function call, and after the function execution the Python error flag is checked. If the error flag is set, a Python exception is raised.

  Thus, this is only useful to call Python C API functions directly.

ctypes.RTLD_GLOBAL

  Valeur possible pour le paramètre mode. Vaut zéro sur les plates-formes où ce drapeau n'est pas disponible.

ctypes.RTLD_LOCAL

  Valeur possible pour le paramètre mode. Vaut RTLD_GLOBAL sur les plates-formes où ce drapeau n'est pas disponible.

ctypes.DEFAULT_MODE

  Mode de chargement par défaut des bibliothèques partagées. Vaut RTLD_GLOBAL sur OSX 10.3 et RTLD_LOCAL sur les autres systèmes d'exploitation.

Shared libraries can also be loaded by using one of the prefabricated objects, which are instances of the `LibraryLoader` class, either by calling the `LoadLibrary()` method, or by retrieving the library as attribute of the loader instance.

class ctypes.LibraryLoader(dlltype)

  Classe pour charger une bibliothèque partagée. dlltype doit être de type `CDLL`, `PyDLL`, `WinDLL` ou `OleDLL`.

  `__getattr__()` has special behavior: It allows loading a shared library by accessing it as attribute of a library loader instance. The result is cached, so repeated attribute accesses return the same library each time.

  LoadLibrary(name)

    Charge une bibliothèque partagée dans le processus et la renvoie. Cette méthode renvoie toujours une nouvelle instance de la bibliothèque.

Plusieurs chargeurs sont fournis :

ctypes.cdll

  Pour créer des instances de `CDLL`.

ctypes.windll

  Creates `WinDLL` instances.

  Disponibilité: Windows

ctypes.oledll

  Creates `OleDLL` instances.

  Disponibilité: Windows

ctypes.pydll

  Pour créer des instances de `PyDLL`.

Il existe un moyen rapide d'accéder directement à l'API C Python :

ctypes.pythonapi

  An instance of `PyDLL` that exposes Python C API functions as attributes. Note that all these functions are assumed to return C int, which is of course not always the truth, so you have to assign the correct `restype` attribute to use these functions.

Charger une bibliothèque à l'aide d'un de ces objets avec en argument `name`, le nom de la bibliothèque (une chaîne de caractères), lève un évènement d'audit `ctypes.dlopen`.

Accéder à une fonction d'une bibliothèque lève un évènement d'audit `ctypes.dlsym` avec `library` (l'objet bibliothèque) et `name` (le nom du symbole — une chaîne de caractères ou un entier) comme arguments.

Si seul le lien sur la bibliothèque, et non l'objet, est disponible, accéder à une fonction lève l'évènement d'audit `ctypes.dlsym/handle` avec `handle` (le lien vers la bibliothèque) et `name` comme arguments.

### Fonctions externes

As explained in the previous section, foreign functions can be accessed as attributes of loaded shared libraries. The function objects created in this way by default accept any number of arguments, accept any ctypes data instances as arguments, and return the default result type specified by the library loader.

They are instances of a private local class `_FuncPtr` (not exposed in `ctypes`) which inherits from the private `_CFuncPtr` class:

    >>> import ctypes
    >>> lib = ctypes.CDLL(None)
    >>> issubclass(lib._FuncPtr, ctypes._CFuncPtr)
    True
    >>> lib._FuncPtr is ctypes._CFuncPtr
    False

class ctypes._CFuncPtr

  Classe de base pour les fonctions externes C.

  Une instance de fonction externe est également un type de donnée compatible avec le C ; elle représente un pointeur vers une fonction.

  Son comportement peut-être personnalisé en réaffectant les attributs spécifiques de l'objet représentant la fonction externe.

  restype

    Assign a ctypes type to specify the result type of the foreign function. Use `None` for void, a function not returning anything.

    It is possible to assign a callable Python object that is not a ctypes type, in this case the function is assumed to return a C int, and the callable will be called with this integer, allowing further processing or error checking. Using this is deprecated, for more flexible post processing or error checking use a ctypes data type as `restype` and assign a callable to the `errcheck` attribute.

  argtypes

    Fait correspondre le type des arguments que la fonction accepte avec un n-uplet de types ctypes. Les fonctions qui utilisent la convention d'appel `stdcall` ne peuvent être appelées qu'avec le même nombre d'arguments que la taille du n-uplet mais les fonctions qui utilisent la convention d'appel C acceptent aussi des arguments additionnels non-définis.

    When a foreign function is called, each actual argument is passed to the `from_param()` class method of the items in the `argtypes` tuple, this method allows adapting the actual argument to an object that the foreign function accepts. For example, a `c_char_p` item in the `argtypes` tuple will convert a string passed as argument into a bytes object using ctypes conversion rules.

    New: It is now possible to put items in argtypes which are not ctypes types, but each item must have a `from_param()` method which returns a value usable as argument (integer, string, ctypes instance). This allows defining adapters that can adapt custom objects as function parameters.

  errcheck

    Définit une fonction Python ou tout autre appelable qui sera appelé avec trois arguments ou plus :

    callable(result, func, arguments)

      result is what the foreign function returns, as specified by the `restype` attribute.

      func est l'objet représentant la fonction externe elle-même. Cet accesseur permet de réutiliser le même appelable pour vérifier le résultat de plusieurs fonctions ou de faire des actions supplémentaires après leur exécution.

      arguments est le n-uplet qui contient les paramètres initiaux passés à la fonction, ceci permet de spécialiser le comportement des arguments utilisés.

    L'objet renvoyé par cette fonction est celui renvoyé par l'appel de la fonction externe, mais il peut aussi vérifier la valeur du résultat et lever une exception si l'appel a échoué.

On Windows, when a foreign function call raises a system exception (for example, due to an access violation), it will be captured and replaced with a suitable Python exception. Further, an auditing event `ctypes.set_exception` with argument `code` will be raised, allowing an audit hook to replace the exception with its own.

Some ways to invoke foreign function calls as well as some of the functions in this module may raise an auditing event `ctypes.call_function` with arguments `function pointer` and `arguments`.

### Prototypes de fonction

Il est aussi possible de créer des fonctions externes en instanciant des prototypes de fonction. Les prototypes de fonction ressemblent beaucoup aux prototypes de fonctions en C ; ils décrivent une fonction (type de retour, type des arguments, convention d'appel) sans préciser son implémentation. Les fabriques de fonctions prennent en entrée le type de retour et le type des arguments de la fonction, et peuvent être utilisées comme des décorateurs-fabrique et ainsi s'appliquer à des fonctions avec la syntaxe `@décorateur`. Ceci est illustré dans la section Fonctions de rappel.

ctypes.CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

  Renvoie un prototype de fonction qui crée des fonctions qui suivent la convention d'appel standard C. Les fonctions libèreront le GIL lors de leur exécution. Si use_errno est vrai, la copie privée ctypes de la variable système `errno` est échangée avec la vraie valeur de `errno` avant et après l'appel ; use_last_error a le même effet sous Windows.

ctypes.WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

  The returned function prototype creates functions that use the `stdcall` calling convention. The function will release the GIL during the call. use_errno and use_last_error have the same meaning as above.

  Disponibilité: Windows

ctypes.PYFUNCTYPE(restype, *argtypes)

  Renvoie un prototype de fonction qui crée des fonctions qui suivent la convention d'appel Python. Les fonctions ne libèreront pas le GIL lors de leur exécution.

Il y a plusieurs façons d'instancier les prototypes de fonction créés par ces fabriques, selon le type et le nombre de paramètres de l'appel :

prototype(address)

  Renvoie une fonction externe sur l'adresse donnée sous la forme d'un entier.

prototype(callable)

  Crée une fonction appelable depuis du code C (une fonction de rappel) d'un appelable Python donné en paramètre.

prototype(func_spec[, paramflags])

  Renvoie une fonction externe exposée par une bibliothèque partagée. func_spec est un couple `(nom_ou_indice, bibliothèque)`. Le premier élément est le nom de la fonction à passer comme une chaîne ou bien son indice (dans la table des symboles) à passer comme un entier. Le second élément est l'instance de la bibliothèque partagée.

prototype(vtbl_index, name[, paramflags[, iid]])

  Renvoie une fonction qui appelle une méthode COM. vtbl_index est l'indice de la fonction dans la table virtuelle, un petit entier positif. name est le nom de la méthode COM. iid est un pointeur optionnel vers l'identificateur de plateforme, qui est utilisé dans la remontée d'erreurs étendue.

  If iid is not specified, an `OSError` is raised if the COM method call fails. If iid is specified, a `COMError` is raised instead.

  COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that are specified in the `argtypes` tuple.

  Disponibilité: Windows

Le paramètre optionnel paramflags crée une fabrique de fonction externes avec des fonctionnalités supplémentaires par rapport à celles décrites ci-dessus.

paramflags must be a tuple of the same length as `argtypes`.

Chaque élément de ce n-uplet contient des informations supplémentaires sur le paramètre correspondant. Ce doit être aussi un n-uplet, avec un, deux ou trois éléments.

Le premier élément est un entier qui contient une combinaison de drapeaux qui précisent le sens des paramètres (entrée ou sortie) :

> 1

>   Paramètre d'entrée.

> 2

>   Paramètre de sortie. La fonction externe va modifier cette valeur.

> 4

>   Paramètre d'entrée, valant 0 par défaut.

Le deuxième élément (optionnel) est une chaîne de caractères représentant le nom du paramètre. Si cet élément est donné, la fonction externe pourra être appelée avec des paramètres nommés.

Le troisième élément (optionnel) est la valeur par défaut du paramètre.

The following example demonstrates how to wrap the Windows `MessageBoxW` function so that it supports default parameters and named arguments. The C declaration from the windows header file is this:

    WINUSERAPI int WINAPI
    MessageBoxW(
        HWND hWnd,
        LPCWSTR lpText,
        LPCWSTR lpCaption,
        UINT uType);

Here is the wrapping with `ctypes`:

    >>> from ctypes import c_int, WINFUNCTYPE, windll
    >>> from ctypes.wintypes import HWND, LPCWSTR, UINT
    >>> prototype = WINFUNCTYPE(c_int, HWND, LPCWSTR, LPCWSTR, UINT)
    >>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", "Hello from ctypes"), (1, "flags", 0)
    >>> MessageBox = prototype(("MessageBoxW", windll.user32), paramflags)

La fonction `MessageBox` peut désormais être appelée des manières suivantes :

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

L'exemple qui suit traite des paramètres en sortie. La fonction win32 `GetWindowRect` donne les dimensions d'une fenêtre en les copiant dans une structure `RECT` que l'appelant doit fournir. Sa déclaration en C est :

    WINUSERAPI BOOL WINAPI
    GetWindowRect(
         HWND hWnd,
         LPRECT lpRect);

Here is the wrapping with `ctypes`:

    >>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
    >>> from ctypes.wintypes import BOOL, HWND, RECT
    >>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
    >>> paramflags = (1, "hwnd"), (2, "lprect")
    >>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
    >>>

Les fonctions avec des paramètres en sortie renvoient automatiquement la valeur du paramètre de sortie s'il n'y en a qu'un seul, ou un n-uplet avec les valeurs de sortie de chaque paramètre s'il y en a plusieurs. Ici, la fonction GetWindowRect renvoie donc une instance de RECT quand elle est appelée.

Output parameters can be combined with the `errcheck` protocol to do further output processing and error checking. The win32 `GetWindowRect` api function returns a `BOOL` to signal success or failure, so this function could do the error checking, and raises an exception when the api call failed:

    >>> def errcheck(result, func, args):
    ...     if not result:
    ...         raise WinError()
    ...     return args
    ...
    >>> GetWindowRect.errcheck = errcheck
    >>>

If the `errcheck` function returns the argument tuple it receives unchanged, `ctypes` continues the normal processing it does on the output parameters. If you want to return a tuple of window coordinates instead of a `RECT` instance, you can retrieve the fields in the function and return them instead, the normal processing will no longer take place:

    >>> def errcheck(result, func, args):
    ...     if not result:
    ...         raise WinError()
    ...     rc = args[1]
    ...     return rc.left, rc.top, rc.bottom, rc.right
    ...
    >>> GetWindowRect.errcheck = errcheck
    >>>

### Fonctions utilitaires

ctypes.addressof(obj)

  Returns the address of the memory buffer as integer. obj must be an instance of a ctypes type.

  Raises an auditing event `ctypes.addressof` with argument `obj`.

ctypes.alignment(obj_or_type)

  Returns the alignment requirements of a ctypes type. obj_or_type must be a ctypes type or instance.

ctypes.byref(obj[, offset])

  Returns a light-weight pointer to obj, which must be an instance of a ctypes type. offset defaults to zero, and must be an integer that will be added to the internal pointer value.

  `byref(obj, offset)` corresponds to this C code:

      (((char *)&obj) + offset)

  The returned object can only be used as a foreign function call parameter. It behaves similar to `pointer(obj)`, but the construction is a lot faster.

ctypes.CopyComPointer(src, dst)

  Copies a COM pointer from src to dst and returns the Windows specific `HRESULT` value.

  If src is not `NULL`, its `AddRef` method is called, incrementing the reference count.

  In contrast, the reference count of dst will not be decremented before assigning the new value. Unless dst is `NULL`, the caller is responsible for decrementing the reference count by calling its `Release` method when necessary.

  Disponibilité: Windows

  Ajouté dans la version 3.14.

ctypes.cast(obj, type)

  This function is similar to the cast operator in C. It returns a new instance of type which points to the same memory block as obj. type must be a pointer type, and obj must be an object that can be interpreted as a pointer.

ctypes.create_string_buffer(init, size=None)
ctypes.create_string_buffer(size)

  This function creates a mutable character buffer. The returned object is a ctypes array of `c_char`.

  If size is given (and not `None`), it must be an `int`. It specifies the size of the returned array.

  If the init argument is given, it must be `bytes`. It is used to initialize the array items. Bytes not initialized this way are set to zero (NUL).

  If size is not given (or if it is `None`), the buffer is made one element larger than init, effectively adding a NUL terminator.

  If both arguments are given, size must not be less than `len(init)`.

  Avertissement

  If size is equal to `len(init)`, a NUL terminator is not added. Do not treat such a buffer as a C string.

  Par exemple :

      >>> bytes(create_string_buffer(2))
      b'\x00\x00'
      >>> bytes(create_string_buffer(b'ab'))
      b'ab\x00'
      >>> bytes(create_string_buffer(b'ab', 2))
      b'ab'
      >>> bytes(create_string_buffer(b'ab', 4))
      b'ab\x00\x00'
      >>> bytes(create_string_buffer(b'abcdef', 2))
      Traceback (most recent call last):
         ...
      ValueError: byte string too long

  Raises an auditing event `ctypes.create_string_buffer` with arguments `init`, `size`.

ctypes.create_unicode_buffer(init, size=None)
ctypes.create_unicode_buffer(size)

  This function creates a mutable unicode character buffer. The returned object is a ctypes array of `c_wchar`.

  The function takes the same arguments as `create_string_buffer()` except init must be a string and size counts `c_wchar`.

  Raises an auditing event `ctypes.create_unicode_buffer` with arguments `init`, `size`.

ctypes.DllCanUnloadNow()

  This function is a hook which allows implementing in-process COM servers with ctypes. It is called from the DllCanUnloadNow function that the _ctypes extension dll exports.

  Disponibilité: Windows

ctypes.DllGetClassObject()

  This function is a hook which allows implementing in-process COM servers with ctypes. It is called from the DllGetClassObject function that the `_ctypes` extension dll exports.

  Disponibilité: Windows

ctypes.util.find_library(name)

  Try to find a library and return a pathname. name is the library name without any prefix like `lib`, suffix like `.so`, `.dylib` or version number (this is the form used for the posix linker option `-l`). If no library can be found, returns `None`.

  Le mode opératoire exact dépend du système.

  See Recherche de bibliothèques partagées for complete documentation.

ctypes.util.find_msvcrt()

  Returns the filename of the VC runtime library used by Python, and by the extension modules. If the name of the library cannot be determined, `None` is returned.

  If you need to free memory, for example, allocated by an extension module with a call to the `free(void *)`, it is important that you use the function in the same library that allocated the memory.

  Disponibilité: Windows

ctypes.util.dllist()

  Try to provide a list of paths of the shared libraries loaded into the current process. These paths are not normalized or processed in any way. The function can raise `OSError` if the underlying platform APIs fail. The exact functionality is system dependent.

  On most platforms, the first element of the list represents the current executable file. It may be an empty string.

  Disponibilité: Windows, macOS, iOS, glibc, BSD libc, musl

  Ajouté dans la version 3.14.

ctypes.FormatError([code])

  Returns a textual description of the error code code. If no error code is specified, the last error code is used by calling the Windows API function `GetLastError()`.

  Disponibilité: Windows

ctypes.GetLastError()

  Returns the last error code set by Windows in the calling thread. This function calls the Windows `GetLastError()` function directly, it does not return the ctypes-private copy of the error code.

  Disponibilité: Windows

ctypes.get_errno()

  Returns the current value of the ctypes-private copy of the system `errno` variable in the calling thread.

  Raises an auditing event `ctypes.get_errno` with no arguments.

ctypes.get_last_error()

  Returns the current value of the ctypes-private copy of the system `LastError` variable in the calling thread.

  Disponibilité: Windows

  Raises an auditing event `ctypes.get_last_error` with no arguments.

ctypes.memmove(dst, src, count)

  Same as the standard C memmove library function: copies count bytes from src to dst. dst and src must be integers or ctypes instances that can be converted to pointers.

ctypes.memset(dst, c, count)

  Same as the standard C memset library function: fills the memory block at address dst with count bytes of value c. dst must be an integer specifying an address, or a ctypes instance.

ctypes.POINTER(type, /)

  Create or return a ctypes pointer type. Pointer types are cached and reused internally, so calling this function repeatedly is cheap. type must be a ctypes type.

  Particularité de l'implémentation CPython : The resulting pointer type is cached in the `__pointer_type__` attribute of type. It is possible to set this attribute before the first call to `POINTER` in order to set a custom pointer type. However, doing this is discouraged: manually creating a suitable pointer type is difficult without relying on implementation details that may change in future Python versions.

ctypes.pointer(obj, /)

  Create a new pointer instance, pointing to obj. The returned object is of the type `POINTER(type(obj))`.

  Note: If you just want to pass a pointer to an object to a foreign function call, you should use `byref(obj)` which is much faster.

ctypes.resize(obj, size)

  This function resizes the internal memory buffer of obj, which must be an instance of a ctypes type. It is not possible to make the buffer smaller than the native size of the objects type, as given by `sizeof(type(obj))`, but it is possible to enlarge the buffer.

ctypes.set_errno(value)

  Set the current value of the ctypes-private copy of the system `errno` variable in the calling thread to value and return the previous value.

  Raises an auditing event `ctypes.set_errno` with argument `errno`.

ctypes.set_last_error(value)

  Sets the current value of the ctypes-private copy of the system `LastError` variable in the calling thread to value and return the previous value.

  Disponibilité: Windows

  Raises an auditing event `ctypes.set_last_error` with argument `error`.

ctypes.sizeof(obj_or_type)

  Returns the size in bytes of a ctypes type or instance memory buffer. Does the same as the C `sizeof` operator.

ctypes.string_at(ptr, size=-1)

  Return the byte string at void *ptr. If size is specified, it is used as size, otherwise the string is assumed to be zero-terminated.

  Raises an auditing event `ctypes.string_at` with arguments `ptr`, `size`.

ctypes.WinError(code=None, descr=None)

  Creates an instance of `OSError`. If code is not specified, `GetLastError()` is called to determine the error code. If descr is not specified, `FormatError()` is called to get a textual description of the error.

  Disponibilité: Windows

  Modifié dans la version 3.3: An instance of `WindowsError` used to be created, which is now an alias of `OSError`.

ctypes.wstring_at(ptr, size=-1)

  Return the wide-character string at void *ptr. If size is specified, it is used as the number of characters of the string, otherwise the string is assumed to be zero-terminated.

  Raises an auditing event `ctypes.wstring_at` with arguments `ptr`, `size`.

ctypes.memoryview_at(ptr, size, readonly=False)

  Return a `memoryview` object of length size that references memory starting at void *ptr.

  If readonly is true, the returned `memoryview` object can not be used to modify the underlying memory. (Changes made by other means will still be reflected in the returned object.)

  This function is similar to `string_at()` with the key difference of not making a copy of the specified memory. It is a semantically equivalent (but more efficient) alternative to `memoryview((c_byte * size).from_address(ptr))`. (While `from_address()` only takes integers, ptr can also be given as a `ctypes.POINTER` or a `byref()` object.)

  Raises an auditing event `ctypes.memoryview_at` with arguments `address`, `size`, `readonly`.

  Ajouté dans la version 3.14.

### Types de données

class ctypes._CData

  This non-public class is the common base class of all ctypes data types. Among other things, all ctypes type instances contain a memory block that hold C compatible data; the address of the memory block is returned by the `addressof()` helper function. Another instance variable is exposed as `_objects`; this contains other Python objects that need to be kept alive in case the memory block contains pointers.

  Common methods of ctypes data types, these are all class methods (to be exact, they are methods of the metaclass):

  from_buffer(source[, offset])

    This method returns a ctypes instance that shares the buffer of the source object. The source object must support the writeable buffer interface. The optional offset parameter specifies an offset into the source buffer in bytes; the default is zero. If the source buffer is not large enough a `ValueError` is raised.

    Raises an auditing event `ctypes.cdata/buffer` with arguments `pointer`, `size`, `offset`.

  from_buffer_copy(source[, offset])

    This method creates a ctypes instance, copying the buffer from the source object buffer which must be readable. The optional offset parameter specifies an offset into the source buffer in bytes; the default is zero. If the source buffer is not large enough a `ValueError` is raised.

    Raises an auditing event `ctypes.cdata/buffer` with arguments `pointer`, `size`, `offset`.

  from_address(address)

    This method returns a ctypes type instance using the memory specified by address which must be an integer.

    This method, and others that indirectly call this method, raises an auditing event `ctypes.cdata` with argument `address`.

  from_param(obj)

    This method adapts obj to a ctypes type. It is called with the actual object used in a foreign function call when the type is present in the foreign function's `argtypes` tuple; it must return an object that can be used as a function call parameter.

    All ctypes data types have a default implementation of this classmethod that normally returns obj if that is an instance of the type. Some types accept other objects as well.

  in_dll(library, name)

    This method returns a ctypes type instance exported by a shared library. name is the name of the symbol that exports the data, library is the loaded shared library.

  Common class variables of ctypes data types:

  __pointer_type__

    The pointer type that was created by calling `POINTER()` for corresponding ctypes data type. If a pointer type was not yet created, the attribute is missing.

    Ajouté dans la version 3.14.

  Common instance variables of ctypes data types:

  _b_base_

    Sometimes ctypes data instances do not own the memory block they contain, instead they share part of the memory block of a base object. The `_b_base_` read-only member is the root ctypes object that owns the memory block.

  _b_needsfree_

    This read-only variable is true when the ctypes data instance has allocated the memory block itself, false otherwise.

  _objects

    This member is either `None` or a dictionary containing Python objects that need to be kept alive so that the memory block contents is kept valid. This object is only exposed for debugging; never modify the contents of this dictionary.

### Types de données de base

class ctypes._SimpleCData

  This non-public class is the base class of all fundamental ctypes data types. It is mentioned here because it contains the common attributes of the fundamental ctypes data types. `_SimpleCData` is a subclass of `_CData`, so it inherits their methods and attributes. ctypes data types that are not and do not contain pointers can now be pickled.

  Instances have a single attribute:

  value

    This attribute contains the actual value of the instance. For integer and pointer types, it is an integer, for character types, it is a single character bytes object or string, for character pointer types it is a Python bytes object or string.

    When the `value` attribute is retrieved from a ctypes instance, usually a new object is returned each time. `ctypes` does not implement original object return, always a new object is constructed. The same is true for all other ctypes object instances.

  Each subclass has a class attribute:

  _type_

    Class attribute that contains an internal type code, as a single-character string. See Types de données de base for a summary.

    Types marked * in the summary may be (or always are) aliases of a different `_SimpleCData` subclass, and will not necessarily use the listed type code. For example, if the platform's long, long long and time_t C types are the same, then `c_long`, `c_longlong` and `c_time_t` all refer to a single class, `c_long`, whose `_type_` code is `'l'`. The `'L'` code will be unused.

    Voir aussi

    The `array` and struct modules, as well as third-party modules like numpy, use similar -- but slightly different -- type codes.

Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a `restype` of `c_char_p`, you will always receive a Python bytes object, not a `c_char_p` instance.

Subclasses of fundamental data types do not inherit this behavior. So, if a foreign functions `restype` is a subclass of `c_void_p`, you will receive an instance of this subclass from the function call. Of course, you can get the value of the pointer by accessing the `value` attribute.

These are the fundamental ctypes data types:

class ctypes.c_byte

  Represents the C signed char datatype, and interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_char

  Represents the C char datatype, and interprets the value as a single character. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class ctypes.c_char_p

  Represents the C char* datatype when it points to a zero-terminated string. For a general character pointer that may also point to binary data, `POINTER(c_char)` must be used. The constructor accepts an integer address, or a bytes object.

class ctypes.c_double

  Represents the C double datatype. The constructor accepts an optional float initializer.

class ctypes.c_longdouble

  Represents the C long double datatype. The constructor accepts an optional float initializer. On platforms where `sizeof(long double) == sizeof(double)` it is an alias to `c_double`.

class ctypes.c_float

  Represents the C float datatype. The constructor accepts an optional float initializer.

class ctypes.c_double_complex

  Represents the C double complex datatype, if available. The constructor accepts an optional `complex` initializer.

  Ajouté dans la version 3.14.

class ctypes.c_float_complex

  Represents the C float complex datatype, if available. The constructor accepts an optional `complex` initializer.

  Ajouté dans la version 3.14.

class ctypes.c_longdouble_complex

  Represents the C long double complex datatype, if available. The constructor accepts an optional `complex` initializer.

  Ajouté dans la version 3.14.

class ctypes.c_int

  Represents the C signed int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where `sizeof(int) == sizeof(long)` it is an alias to `c_long`.

class ctypes.c_int8

  Represents the C 8-bit signed int datatype. It is an alias for `c_byte`.

class ctypes.c_int16

  Represents the C 16-bit signed int datatype. Usually an alias for `c_short`.

class ctypes.c_int32

  Represents the C 32-bit signed int datatype. Usually an alias for `c_int`.

class ctypes.c_int64

  Represents the C 64-bit signed int datatype. Usually an alias for `c_longlong`.

class ctypes.c_long

  Represents the C signed long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_longlong

  Represents the C signed long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where `sizeof(long long) == sizeof(long)` it is an alias to `c_long`.

class ctypes.c_short

  Represents the C signed short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_size_t

  Represents the C `size_t` datatype. Usually an alias for another unsigned integer type.

class ctypes.c_ssize_t

  Represents the `Py_ssize_t` datatype. This is a signed version of `size_t`; that is, the POSIX `ssize_t` type. Usually an alias for another integer type.

  Ajouté dans la version 3.2.

class ctypes.c_time_t

  Represents the C `time_t` datatype. Usually an alias for another integer type.

  Ajouté dans la version 3.12.

class ctypes.c_ubyte

  Represents the C unsigned char datatype, it interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_uint

  Represents the C unsigned int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where `sizeof(int) == sizeof(long)` it is an alias for `c_ulong`.

class ctypes.c_uint8

  Represents the C 8-bit unsigned int datatype. It is an alias for `c_ubyte`.

class ctypes.c_uint16

  Represents the C 16-bit unsigned int datatype. Usually an alias for `c_ushort`.

class ctypes.c_uint32

  Represents the C 32-bit unsigned int datatype. Usually an alias for `c_uint`.

class ctypes.c_uint64

  Represents the C 64-bit unsigned int datatype. Usually an alias for `c_ulonglong`.

class ctypes.c_ulong

  Represents the C unsigned long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_ulonglong

  Represents the C unsigned long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where `sizeof(long long) == sizeof(long)` it is an alias to `c_long`.

class ctypes.c_ushort

  Represents the C unsigned short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_void_p

  Represents the C void* type. The value is represented as integer. The constructor accepts an optional integer initializer.

class ctypes.c_wchar

  Represents the C `wchar_t` datatype, and interprets the value as a single character unicode string. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class ctypes.c_wchar_p

  Represents the C wchar_t* datatype, which must be a pointer to a zero-terminated wide character string. The constructor accepts an integer address, or a string.

class ctypes.c_bool

  Represent the C bool datatype (more accurately, _Bool from C99). Its value can be `True` or `False`, and the constructor accepts any object that has a truth value.

class ctypes.HRESULT

  Represents a `HRESULT` value, which contains success or error information for a function or method call.

  Disponibilité: Windows

class ctypes.py_object

  Represents the C PyObject* datatype. Calling this without an argument creates a `NULL` PyObject* pointer.

  Modifié dans la version 3.14: `py_object` is now a generic type.

The `ctypes.wintypes` module provides quite some other Windows specific data types, for example `HWND`, `WPARAM`, `VARIANT_BOOL` or `DWORD`. Some useful structures like `MSG` or `RECT` are also defined.

### Types de données dérivés de Structure

class ctypes.Union(*args, **kw)

  Abstract base class for unions in native byte order.

  Unions share common attributes and behavior with structures; see `Structure` documentation for details.

class ctypes.BigEndianUnion(*args, **kw)

  Abstract base class for unions in big endian byte order.

  Ajouté dans la version 3.11.

class ctypes.LittleEndianUnion(*args, **kw)

  Abstract base class for unions in little endian byte order.

  Ajouté dans la version 3.11.

class ctypes.BigEndianStructure(*args, **kw)

  Abstract base class for structures in big endian byte order.

class ctypes.LittleEndianStructure(*args, **kw)

  Abstract base class for structures in little endian byte order.

Structures and unions with non-native byte order cannot contain pointer type fields, or any other data types containing pointer type fields.

class ctypes.Structure(*args, **kw)

  Abstract base class for structures in native byte order.

  Concrete structure and union types must be created by subclassing one of these types, and at least define a `_fields_` class variable. `ctypes` will create descriptors which allow reading and writing the fields by direct attribute accesses. These are the

  _fields_

    A sequence defining the structure fields. The items must be 2-tuples or 3-tuples. The first item is the name of the field, the second item specifies the type of the field; it can be any ctypes data type.

    For integer type fields like `c_int`, a third optional item can be given. It must be a small positive integer defining the bit width of the field.

    Field names must be unique within one structure or union. This is not checked, only one field can be accessed when names are repeated.

    It is possible to define the `_fields_` class variable after the class statement that defines the Structure subclass, this allows creating data types that directly or indirectly reference themselves:

        class List(Structure):
            pass
        List._fields_ = [("pnext", POINTER(List)),
                         ...
                        ]

    The `_fields_` class variable can only be set once. Later assignments will raise an `AttributeError`.

    Additionally, the `_fields_` class variable must be defined before the structure or union type is first used: an instance or subclass is created, `sizeof()` is called on it, and so on. Later assignments to `_fields_` will raise an `AttributeError`. If `_fields_` has not been set before such use, the structure or union will have no own fields, as if `_fields_` was empty.

    Sub-subclasses of structure types inherit the fields of the base class plus the `_fields_` defined in the sub-subclass, if any.

  _pack_

    An optional small integer that allows overriding the alignment of structure fields in the instance.

    This is only implemented for the MSVC-compatible memory layout (see `_layout_`).

    Setting `_pack_` to 0 is the same as not setting it at all. Otherwise, the value must be a positive power of two. The effect is equivalent to `#pragma pack(N)` in C, except `ctypes` may allow larger n than what the compiler accepts.

    `_pack_` must already be defined when `_fields_` is assigned, otherwise it will have no effect.

    Deprecated since version 3.14, will be removed in version 3.19: For historical reasons, if `_pack_` is non-zero, the MSVC-compatible layout will be used by default. On non-Windows platforms, this default is deprecated and is slated to become an error in Python 3.19. If it is intended, set `_layout_` to `'ms'` explicitly.

  _align_

    An optional small integer that allows increasing the alignment of the structure when being packed or unpacked to/from memory.

    The value must not be negative. The effect is equivalent to `__attribute__((aligned(N)))` on GCC or `#pragma align(N)` on MSVC, except `ctypes` may allow values that the compiler would reject.

    `_align_` can only increase a structure's alignment requirements. Setting it to 0 or 1 has no effect.

    Using values that are not powers of two is discouraged and may lead to surprising behavior.

    `_align_` must already be defined when `_fields_` is assigned, otherwise it will have no effect.

    Ajouté dans la version 3.13.

  _layout_

    An optional string naming the struct/union layout. It can currently be set to:

    - `"ms"`: the layout used by the Microsoft compiler (MSVC). On GCC and Clang, this layout can be selected with `__attribute__((ms_struct))`.

    - `"gcc-sysv"`: the layout used by GCC with the System V or “SysV-like” data model, as used on Linux and macOS. With this layout, `_pack_` must be unset or zero.

    If not set explicitly, `ctypes` will use a default that matches the platform conventions. This default may change in future Python releases (for example, when a new platform gains official support, or when a difference between similar platforms is found). Currently the default will be:

    - On Windows: `"ms"`

    - When `_pack_` is specified: `"ms"`. (This is deprecated; see `_pack_` documentation.)

    - Otherwise: `"gcc-sysv"`

    `_layout_` must already be defined when `_fields_` is assigned, otherwise it will have no effect.

    Ajouté dans la version 3.14.

  _anonymous_

    An optional sequence that lists the names of unnamed (anonymous) fields. `_anonymous_` must be already defined when `_fields_` is assigned, otherwise it will have no effect.

    The fields listed in this variable must be structure or union type fields. `ctypes` will create descriptors in the structure type that allows accessing the nested fields directly, without the need to create the structure or union field.

    Here is an example type (Windows):

        class _U(Union):
            _fields_ = [("lptdesc", POINTER(TYPEDESC)),
                        ("lpadesc", POINTER(ARRAYDESC)),
                        ("hreftype", HREFTYPE)]

        class TYPEDESC(Structure):
            _anonymous_ = ("u",)
            _fields_ = [("u", _U),
                        ("vt", VARTYPE)]

    The `TYPEDESC` structure describes a COM data type, the `vt` field specifies which one of the union fields is valid. Since the `u` field is defined as anonymous field, it is now possible to access the members directly off the TYPEDESC instance. `td.lptdesc` and `td.u.lptdesc` are equivalent, but the former is faster since it does not need to create a temporary union instance:

        td = TYPEDESC()
        td.vt = VT_PTR
        td.lptdesc = POINTER(some_type)
        td.u.lptdesc = POINTER(some_type)

  It is possible to define sub-subclasses of structures, they inherit the fields of the base class. If the subclass definition has a separate `_fields_` variable, the fields specified in this are appended to the fields of the base class.

  Structure and union constructors accept both positional and keyword arguments. Positional arguments are used to initialize member fields in the same order as they are appear in `_fields_`. Keyword arguments in the constructor are interpreted as attribute assignments, so they will initialize `_fields_` with the same name, or create new attributes for names not present in `_fields_`.

class ctypes.CField(*args, **kw)

  Descriptor for fields of a `Structure` and `Union`. For example:

      >>> class Color(Structure):
      ...     _fields_ = (
      ...         ('red', c_uint8),
      ...         ('green', c_uint8),
      ...         ('blue', c_uint8),
      ...         ('intense', c_bool, 1),
      ...         ('blinking', c_bool, 1),
      ...    )
      ...
      >>> Color.red
      <ctypes.CField 'red' type=c_ubyte, ofs=0, size=1>
      >>> Color.green.type
      <class 'ctypes.c_ubyte'>
      >>> Color.blue.byte_offset
      2
      >>> Color.intense
      <ctypes.CField 'intense' type=c_bool, ofs=3, bit_size=1, bit_offset=0>
      >>> Color.blinking.bit_offset
      1

  All attributes are read-only.

  `CField` objects are created via `_fields_`; do not instantiate the class directly.

  Ajouté dans la version 3.14: Previously, descriptors only had `offset` and `size` attributes and a readable string representation; the `CField` class was not available directly.

  name

    Name of the field, as a string.

  type

    Type of the field, as a ctypes class.

  offset
  byte_offset

    Offset of the field, in bytes.

    For bitfields, this is the offset of the underlying byte-aligned storage unit; see `bit_offset`.

  byte_size

    Size of the field, in bytes.

    For bitfields, this is the size of the underlying storage unit. Typically, it has the same size as the bitfield's type.

  size

    For non-bitfields, equivalent to `byte_size`.

    For bitfields, this contains a backwards-compatible bit-packed value that combines `bit_size` and `bit_offset`. Prefer using the explicit attributes instead.

  is_bitfield

    True if this is a bitfield.

  bit_offset
  bit_size

    The location of a bitfield within its storage unit, that is, within `byte_size` bytes of memory starting at `byte_offset`.

    To get the field's value, read the storage unit as an integer, shift left by `bit_offset` and take the `bit_size` least significant bits.

    For non-bitfields, `bit_offset` is zero and `bit_size` is equal to `byte_size * 8`.

  is_anonymous

    True if this field is anonymous, that is, it contains nested sub-fields that should be merged into a containing structure or union.

### Tableaux et pointeurs

class ctypes.Array(*args)

  Classe de base abstraite pour les arrays.

  The recommended way to create concrete array types is by multiplying any `ctypes` data type with a non-negative integer. Alternatively, you can subclass this type and define `_length_` and `_type_` class variables. Array elements can be read and written using standard subscript and slice accesses; for slice reads, the resulting object is not itself an `Array`.

  _length_

    A positive integer specifying the number of elements in the array. Out-of-range subscripts result in an `IndexError`. Will be returned by `len()`.

  _type_

    Spécifie le type de chaque élément de l'array.

  Array subclass constructors accept positional arguments, used to initialize the elements in order.

ctypes.ARRAY(type, length)

  Create an array. Equivalent to `type * length`, where type is a `ctypes` data type and length an integer.

  This function is soft deprecated in favor of multiplication. There are no plans to remove it.

class ctypes._Pointer

  Private, abstract base class for pointers.

  Concrete pointer types are created by calling `POINTER()` with the type that will be pointed to; this is done automatically by `pointer()`.

  If a pointer points to an array, its elements can be read and written using standard subscript and slice accesses. Pointer objects have no size, so `len()` will raise `TypeError`. Negative subscripts will read from the memory before the pointer (as in C), and out-of-range subscripts will probably crash with an access violation (if you're lucky).

  _type_

    Specifies the type pointed to.

  contents

    Returns the object to which to pointer points. Assigning to this attribute changes the pointer to point to the assigned object.

### Exceptions

exception ctypes.ArgumentError

  Exception levée quand un appel à la fonction externe ne peut pas convertir un des arguments qu'elle a reçus.

exception ctypes.COMError(hresult, text, details)

  This exception is raised when a COM method call failed.

  hresult

    The integer value representing the error code.

  text

    The error message.

  details

    The 5-tuple `(descr, source, helpfile, helpcontext, progid)`.

    descr is the textual description. source is the language-dependent `ProgID` for the class or application that raised the error. helpfile is the path of the help file. helpcontext is the help context identifier. progid is the `ProgID` of the interface that defined the error.

  Disponibilité: Windows

  Ajouté dans la version 3.14.