The decision depends on whether you need the data type to be changed (from a Python perspective).
If you do not need the data to be modified, then do not worry about the buffer constructor. Just pass the bytes object directly to the TEST_STRUCT constructor. eg.
from ctypes import * class TEST_STRUCT(Structure): _fields_ = [("a", c_int), ("b", c_char_p)] hello_args = TEST_STRUCT(1, b"test")
If you need a mutable buffer, you need to specify your char* type somewhat differently in the TEST_STRUCT class. eg.
from ctypes import * class TEST_STRUCT(Structure): _fields_ = [("a", c_int), ("b", POINTER(c_char))]
c_char_p compared to POINTER(c_char)
Allegedly, although these two seem similar, they are slightly different from each other. POINTER(c_char) - for any char data buffer. c_char_p is for null-terminated strings. As a side effect of this condition, all c_char_p objects c_char_p immutable (from the python side) to simplify this guarantee. eg. {65, 66, 67, 0} is the string ( "ABC" ), while {1, 2, 3} will not.
Dunes source share