Convert C Array Of Pointers To Python Array Of Structures
I am writing a Python app that makes use of PulseAudio API. The implementation is heavily using callbacks written in Python and invoked by PulseAudio's C code. The most information
Solution 1:
Solving this problem required careful reading of Python's ctypes reference. Once the mechanism of ctypes
type translation implementation was clear, it's not so difficult to get to the desired values.
The main idea about pointers is that you use their contents
attribute to get to the data the pointer points to. Another useful thing to know is that pointers can be indexed like arrays (it's not validated by the interpreter, so it's your own responsibility to make sure it is indeed an array).
For this particular PulseAudio example, we can process the ports
structure member (which is a pointer to array of pointers) as follows:
port_list = []
ifstruct.contents.ports:
i = 0while True:
port_ptr = struct.contents.ports[i]
# NULL pointer terminates the array
if port_ptr:
port_struct = port_ptr.contents
port_list.append(port_struct.name)
i += 1else:
break
Post a Comment for "Convert C Array Of Pointers To Python Array Of Structures"