[Python] Using a pointer to fill a ctypes structure

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have need create a ctypes struct using a pointer. Example code

    class MessageDecoder:
    def __init__(self, raw_data : bytes):
    self.raw_data = raw_data
    self.writable_raw_data = None
    self.datagram = None
    self.tag = None

    def decode(self):
    c_array = cast(self.raw_data, POINTER(c_ubyte *
    len(self.raw_data)))[0]
    data_pointer = pointer(c_array)
    self.datagram = Datagram.from_buffer(data_pointer)


    I get the c_array. I can get the pointer. However, trying to create Datagram fails. I can create one using c_array. EX: Datagram.from_buffer(c_array) Datagram looks like:

    class Datagram(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
    ("MagicNumber", c_uint),
    ("VersionMinor", c_uint16),
    ("VersionMajor", c_uint16),
    ("SequenceNumber", c_uint16),
    ("SeqNumberHigh", c_uint16),
    ("DataSize", c_uint32),
    ("Attribute", GenericAttribute)
    ]


    Why?

    Continue reading...

Compartilhe esta Página