Python ctypes Module
❮ Standard Library ModulesExample
Create and use a C integer:
import ctypes
x = ctypes.c_int(7)
x.value += 5
print(x.value)
Try it Yourself »
Definition and Usage
The ctypes module provides C compatible data types and allows calling functions in DLLs/shared libraries.
Use it to wrap native libraries, define C structs, and interoperate with system APIs without writing extension modules.
Members
| Member | Description |
|---|---|
| addressof() | Return the memory address of the argument. |
| alignment() | Return the alignment requirements of a ctypes type or instance. |
| byref() | Return a pass-by-reference object for a ctypes instance. |
| c_bool | C compatible boolean type. |
| c_byte | Signed char. |
| c_char | C char type. |
| c_char_p | C char* (NUL terminated string) type. |
| c_double | C double type. |
| c_float | C float type. |
| c_int | C int type. |
| c_int16 | 16-bit signed integer. |
| c_int8 | 8-bit signed integer. |
| c_long | C long type. |
| c_longlong | C long long (64-bit) type. |
| c_short | C short type. |
| c_size_t | C size_t type. |
| c_ssize_t | C ssize_t type. |
| c_uint | C unsigned int type. |
| c_uint16 | 16-bit unsigned integer. |
| c_uint8 | 8-bit unsigned integer. |
| c_ulong | C unsigned long type. |
| c_ulonglong | C unsigned long long (64-bit) type. |
| c_void_p | C void* type. |
| c_wchar | C wide character. |
| c_wchar_p | C wide char* (UTF-16/32 depending on platform). |
| cast() | Cast a ctypes instance to another ctypes type. |
| cdll | Pre-configured loader for C libraries using cdecl calling convention. |
| CDLL() | Load a shared library (POSIX). |
| CFUNCTYPE() | Create a C function prototype from argument and result types (cdecl). |
| create_string_buffer() | Create a mutable character buffer. |
| create_unicode_buffer() | Create a mutable wide-character buffer. |
| LibraryLoader | Loader helper used by cdll, windll, and oledll. |
| memmove() | Copy memory from one address to another. |
| memset() | Fill a block of memory with a byte value. |
| oledll | Pre-configured loader for stdcall libraries (HRESULT checking). |
| OleDLL() | Load a Windows stdcall DLL and check HRESULT (Windows). |
| pointer() | Create a pointer to a ctypes instance. |
| POINTER() | Create a ctypes pointer type to another ctypes type. |
| PyDLL() | Like CDLL but ensures the GIL is held during calls (for CPython APIs). |
| pythonapi | Handle to the CPython C-API DLL/shared library. |
| sizeof() | Return the size in bytes of a ctypes type or instance. |
| string_at() | Return a bytes object from a memory address and optional length. |
| Structure | Base class for defining C structs. |
| Union | Base class for defining C unions. |
| WinDLL() | Load a Windows stdcall DLL (Windows). |
| windll | Pre-configured loader for stdcall libraries on Windows. |
| wstring_at() | Return a Python string from a wide memory address and optional length. |