ctypes: how to make a structure pointer to point to a buffer

人言落日是天涯,望极天涯不见家 kelvin.you at gmail.com
Mon Apr 23 05:53:54 EDT 2007


On Apr 23, 5:42 pm, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> 人言落日是天涯,望极天涯不见家 wrote:
> > first, I'm try the POINTER to convesion the pointer type. but failed.
>
> > class STUDENT(Structure):
> >     _fields_ = [('name',  c_int),
> >                     ('id',   c_int),
> >                     ('addition',    c_ubyte)]
>
> > buffer = c_byte * 1024
> > student_p = cast(buffer, POINTER(STUDENT))
>
> > The parameter of the POINTER must be ctypes type.
> > How could I attach the buffer pointer to the structure STUDENT ?
>
> I think it should work like this:
>
> from ctypes import *
>
> class STUDENT(Structure):
>     _fields_ = [('name',  c_int),
>                     ('id',   c_int),
>                     ('addition',    c_ubyte)]
>
> buffer = (c_byte * 1024)()
> buffer_p = pointer(buffer)
> student_p = cast(buffer_p, POINTER(STUDENT))
>
> print student_p
>
> Diez


yes, it should add the bracket
buffer = (c_byte * 1024)()

Thank you !



More information about the Python-list mailing list