K
K
kutu2013-12-28 13:03:10
Python
kutu, 2013-12-28 13:03:10

How to map a read-only buffer to ctypes.Structure?

python3.3, win32
has a chunk of memory available for reading

m = mmap.mmap(0, MEMMAPFILESIZE, MEMMAPFILE, access=mmap.ACCESS_READ)

from it I want to get an object with fields for reading from this memory directly, but I don’t want to create a class with a bunch of properties and manual reading at the right offsets
, now I did this
header_struct = struct.Struct('i')
Header = collections.namedtuple('Header', 'version')
h = Header._make(header_struct.unpack_from(m))
print(h.version)

the problem is that the values ​​in memory are dynamic, another program changes them, and this method only copies the memory, besides, auto-completion will not work
if done through Structure
class Header(ctypes.Structure):
    _fields_ = [
        ('version', ctypes.c_int),
    ]
h = Header.from_buffer(m)

this causes an error, since the buffer must be writable,
but I want it to be beautiful and simple

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question