C Structure rebuild with ctypes

Mark Tolonen metolone+gmane at gmail.com
Tue Dec 22 14:09:31 EST 2009


"Georg" <nobody at nowhere.org> wrote in message 
news:7pb8ubFll1U1 at mid.individual.net...
> Hi Mark,
>
> many thanks for your valuable help.
>
>>>> # numVars contains size of returned arrays.  Recast to access.
>>>> varNamesArray = c.cast(varNames,c.POINTER(PCHAR * numVars.value))
>>>> varTypesArray = c.cast(varTypes,c.POINTER(INT * numVars.value))
>
> One last question: You created an object varNamesArray as an ctypes array. 
> This object has a method "contents". How do I find out what other methods 
> this objects has? For instance a method to retrieve the size of the array? 
> Is this documented somewhere?

The "contents" property is how you dereference a ctypes pointer.  See 
"16.15. ctypes" and specifically "16.15.1.14. Pointers" in the Python 2.6.4 
documentation.  "16.15.1.13. Arrays" documents that arrays support len().


>>> import ctypes as c
>>> a = (c.c_int * 5)()
>>> p = c.pointer(a)
>>> p.contents  # p points to an array of 5 integers
<__main__.c_long_Array_5 object at 0x009B46C0>
>>> len(p.contents)   # or just len(a)
5

You can use "dir(p)" or "help(p)" to find out information about methods and 
attributes of an object.  Both show that 'p' has a contents attribute and 
'a' supports the __len__ method.

-Mark





More information about the Python-list mailing list