ctypes initializer

marek.rocki at wp.pl marek.rocki at wp.pl
Sun Aug 24 01:43:34 CEST 2008


castironpi napisał(a):
> Is there a way to initialize a ctypes Structure to point to an offset
> into a buffer? I don't know if the way I'm doing it is supported.

There is a high probability you're abusing ctypes too much, but it's
possible. The following seems to work:

from ctypes import *

class S(Structure):
	_fields_ = [('x', c_uint), ('y', c_int)]
rawdata = create_string_buffer('\xEE\xFF\x78\x56\x34\x12\xFF\xFF\xFF
\xFF\xAA')

# Try to make a structure s of type S which takes its data from
rawdata
# buffer, starting at index 2
s = cast(c_void_p(addressof(rawdata)+2), POINTER(S)).contents

print hex(s.x), s.y # Should be 12345678h and -1



More information about the Python-list mailing list