I need to get a pointer with the address of a struct field. Important: I'm builduing a serializer for a set of c structs so i want to iterate over the fields of a struct and get the address of each of them as a pointer. I know there is a way using fields object and offset property of them which is giving you the offset from the address of the structure itself but it is a generic pointer. Could you show me a way on how to iterate over struct fields and for each of them get a ctypes pointer with a correct inner type?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Here's a way...
The class variable
Test._fields_
defines the attributes and their C types. The class attributes generated by assigning the fields contain information about the offset for that attribute, for example:For each type, the
from_buffer()
method builds a C type using the same data buffer as an existing type. An instance of the structure implements the buffer API required to access its data, so if you know the offset of a structure element's data, you can generate actypes
type that references the same data, and create a pointer to it.Output: