A Question about ctypes and a function f(void **)

Nick Craig-Wood nick at craig-wood.com
Mon Aug 11 16:33:46 EDT 2008


sapsi <saptarshi.guha at gmail.com> wrote:
>  I have a C function f(void**,int *), in which it writes some
>  information (it is a RGB32 image).
> 
>  Here is what i do
>  rowlength=c_int()
>  data=c_void_p()
>  d=pointer(data)
>  f(d,byref(rowlength)
>  The call works (no segmentation fault), now how do i access the data
>  in d? Because i need to pass it to a another function  QImage that
>  takes void* as its first parameter.
> 
>  If i do d.contents i get c_void_p(3067478024L)

You almost answered your own question!

d.contents is a c_void_p ie a (void *) so you can pass d.contents to
QImage directly I would have thought.

If you want to actually read the data from python then you'll need to
cast it to some other pointer type than c_void_p first, eg

  >>> d
  c_void_p(136692916)
  >>> s = c_char_p(d.value)
  >>> s
  c_char_p(136692916)
  >>> s.value
  'hello'
  >>>

or use ctypes.memmove to copy the data out to somewhere else.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list