Re: [ctypes-users] Initialising a struct from a buffer
Brought to you by:
theller
|
From: Thomas H. <th...@ct...> - 2009-03-12 18:33:34
|
Bradley Schatz schrieb: > Hi, > > Is there a simple way to initialise a struct from a buffer containing > bytes, without first copying the contents of the buffer into a ctypes > string buffer? I am currently doing the following to initialise my > object (python 2.5 ctypes). buf is the result of a read on a file > descriptor. > > ptr = > ctypes.create_string_buffer(self.buf[offset:offset+ctypes.sizeof(EndCentralDirectory)]) > self.end = EndCentralDirectory() > ctypes.memmove(ctypes.byref(self.end), ptr, > ctypes.sizeof(EndCentralDirectory)) > > Im sure there is a simpler way to do this, but it is eluding me. Suggestions? Well, Python 2.6's ctypes has .from_buffer and .from_buffer_copy class methods. Since you're using 2.5, you have to get the address of the buffer (as integer) and then pass it to the .from_address class method. The Structure instance will then be valid as long as the source object. -- Thanks, Thomas |