ctypes.c_wchar_p is the ctypes type for a C wchar_t *: a pointer to a null-terminated wide-character string. Use it as an argtypes entry when a C function takes const wchar_t *, and as a restype when it returns one. Python str values convert to it automatically.
The width of wchar_t depends on the platform, 2 bytes on Windows and 4 bytes on Linux and macOS, but ctypes handles the conversion for you, so the same Python code works on all of them.
c_wchar_p points at wide characters. When you assign or pass a Python str, ctypes creates a temporary wide-character copy and passes its address. When you read a returned pointer, ctypes decodes the characters into a new Python str.
The pointer itself is a normal pointer size. What changes with the platform is the size of each character it points to.
Here a C function takes a wide string and returns a greeting built from it.
The returned str is a copy, so it stays valid even though the C buffer is static and gets overwritten by the next call. This also means the example function is not thread-safe, because the static buffer is shared. In real libraries, have the caller supply the output buffer instead.
A Python str is immutable, so you must never pass one to a function that writes into its argument. Allocate a writable buffer with create_unicode_buffer and pass that.
Use c_wchar_p for read-only inputs and for strings that C owns, and a c_wchar array for memory Python provides and C writes. The array form is covered in more detail in the article on ctypes.c_wchar_Array.
| Aspect | c_wchar_p | c_char_p |
|---|---|---|
| C type | wchar_t * | char * |
| Python input | str | bytes |
| Python output | str | bytes |
| Character width | 2 bytes (Windows) or 4 bytes (Linux/macOS) | 1 byte |
| Encoding | Handled by ctypes | You encode and decode yourself |
| Typical use | Windows W APIs, wchar_t libraries | UTF-8 and ASCII libraries |
Use the type that matches the C signature. Passing a str to a char * parameter fails in Python 3, and passing bytes to a wchar_t * parameter fails the same way. If the library uses UTF-8, encode with text.encode("utf-8") and use c_char_p.
str; you do not free anything.malloc), declare restype = ctypes.c_void_p, read the text with ctypes.wstring_at(ptr), then call the library's free function. A c_wchar_p return value would copy the text but lose the address you need for freeing.str argument is released as soon as the call finishes.It converts to the platform's wchar_t: UTF-16 code units on Windows and UTF-32 code points on Linux and macOS. You write the same Python code on both.
Pass None. A returned NULL pointer is read back as None.
Python strings are immutable, and ctypes passes a temporary copy. Use ctypes.create_unicode_buffer(size) for any parameter that C writes into.
Use ctypes.wstring_at(address, length=-1) when you hold the address as a c_void_p or an integer, for example when you must free the memory afterwards.
ctypes.c_wchar_p maps to wchar_t * and converts between Python str and null-terminated wide strings automatically. Set argtypes and restype, use a create_unicode_buffer when C needs to write, and use c_char_p with explicit encoding when the library works with bytes.