You declare a c_wchar_p variable by calling ctypes.c_wchar_p with a Python str, for example name = ctypes.c_wchar_p("Hello"). The object holds a pointer to a NUL-terminated wide-character (wchar_t) string, and the .value attribute gives you the text back as a normal Python str. Calling c_wchar_p() with no argument creates a NULL pointer whose .value is None.
Two details trip people up. The width of wchar_t depends on the platform, and a c_wchar_p points to memory that C code must not modify. For a writable buffer you use ctypes.create_unicode_buffer instead.
ctypes.c_wchar_p maps to the C type wchar_t *. It is a pointer type, not a string container: the characters themselves live in a separate block of memory that the pointer refers to. When you pass a Python str, ctypes allocates that block for you and keeps it alive as long as the c_wchar_p object exists.
The companion types are worth knowing:
| ctypes type | C type | Typical use |
|---|---|---|
c_wchar_p | wchar_t * | Wide string passed in or returned by a function |
c_char_p | char * | Byte string (bytes), usually ASCII or UTF-8 |
c_wchar | wchar_t | A single wide character |
create_unicode_buffer(...) | wchar_t[] | Writable wide-character buffer |
Create the variable by passing a string, then read it through .value:
You can also create an empty pointer and assign to it later:
Assigning a new str to .value makes the pointer refer to a new block of memory. It does not edit the characters of the old one, so any other pointer that still refers to the old block is unaffected.
The original idea that c_wchar_p is always UTF-16 is only true on Windows. The size of wchar_t is set by the platform and compiler:
| Platform | wchar_t size | Encoding in memory |
|---|---|---|
| Windows | 2 bytes | UTF-16 code units |
| Linux and macOS | 4 bytes | UTF-32 code points |
ctypes handles the conversion between Python's str and the native layout, so you rarely need to care. It matters when you compute buffer sizes or read raw bytes, and when a C library expects a fixed encoding such as UTF-16 on Linux. In that case wchar_t is the wrong tool and you should pass encoded bytes instead.
Most real code uses c_wchar_p to describe function signatures. Setting argtypes and restype lets ctypes convert arguments and results for you, and it avoids silent pointer truncation on 64-bit systems.
With argtypes set, you can pass a plain Python str directly and ctypes wraps it in a c_wchar_p for the call. Windows functions that end in W take wide strings, while the A variants take char *.
When a function returns a wchar_t *, declare restype = ctypes.c_wchar_p. ctypes then converts the result to a str automatically. If you need the raw address to free it later, use ctypes.c_void_p as the return type and convert it yourself.
A c_wchar_p made from a Python str points to read-only memory as far as C is concerned. If a function must write into the string, which is the usual pattern for "fill this buffer" APIs, allocate a mutable buffer:
create_unicode_buffer also accepts an initial string, as in create_unicode_buffer("abc", 10), where the second argument is the total size in characters. Always pass the buffer size to the C function so it cannot write past the end.
c_wchar_p where the API wants char *. The result is garbled text or an empty string, because every character is read with the wrong width.bytes to a c_wchar_p parameter. ctypes raises TypeError; decode the bytes first or use c_char_p.c_wchar_p or buffer for as long as the C side may use it.argtypes. Without it ctypes guesses the types, and on 64-bit systems a returned pointer can be truncated to a 32-bit integer.Read the .value attribute. It returns a str, or None if the pointer is NULL.
c_wchar_p points to wide characters (wchar_t) and exchanges str values, while c_char_p points to single-byte characters and exchanges bytes. Pick the one that matches the C prototype.
Not safely. Use ctypes.create_unicode_buffer to get writable memory and pass that to the function, then read the result from buffer.value.
Only on Windows, where wchar_t is 2 bytes. On Linux and macOS it is 4 bytes and holds UTF-32 code points, so do not rely on a UTF-16 layout in portable code.
Declare a wide string pointer with ctypes.c_wchar_p("text") and read it back through .value. Remember that wchar_t is 2 bytes on Windows and 4 bytes elsewhere, set argtypes and restype on every foreign function, and use create_unicode_buffer whenever C code needs to write into the string. Keep the Python object alive for as long as C code may use its pointer.