ctypes reference

overview :: tutorial :: reference :: faq

( Work in progress: COM :: COM sample )

Common operations on ctypes instances

sizeof(obj) -> integer

Return an integer specifying the number of bytes in the memory block. This corresponds to the C code sizeof(obj). Does also work for types.

byref(obj) -> cparam-object

Similar to &obj in C code, but only usable for passing the object as a parameter to a C function call.

pointer(obj) -> ctypes instance

Same as &obj in C. Different from byref(obj), this creates a new ctypes object, acting as a pointer to obj. It will be an instance of a pointer class pointing to type(obj) instances. The pointer class is created on the fly, if it does not yet exist.

Common operation on ctypes types

ctypes types/classes extensively use the new Python type system, so the following may first look unusual to the Pyhon programmer.

POINTER(ctype) -> class

Return a subclass of ctype. Instances of this class are pointers to ctype. The subclass will be created if it does not yet exist, otherwise it is returned from a builtin cache.

ctype * num -> array class

Multiplying a ctype class with a positive integer creates a subclass of Array. Instances are arrays holding num instances of ctype.

ctype.from_address(address) -> ctypes instance

Create a new instance of ctype from the memory block at the integer address. It must no longer be accessed of the memory block becomes invalid, the programmer must ensure this.

ctypes instance methods/properties

obj._as_parameter_ -> magic

Automatically called when a ctype object is used as a parameter in a C function call by value. Must return something that can be understood by the C function call parameter converter. Currently, it either returns an integer, a string or a unicode string. These are passed as the corresponding C data type to the function call.



Page updated: Wed Jun 25 23:16:06 2003