Passing FILE * types using ctypes

geremy condra debatem1 at gmail.com
Wed Mar 3 21:04:32 EST 2010


On Wed, Mar 3, 2010 at 6:50 PM, Zeeshan Quireshi
<zeeshan.quireshi at gmail.com> wrote:
> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer, how do i open a file in Python and convert it to a
> FILE *pointer. Or do i have to call the C library using ctypes first,
> get the pointer and then pass it to my function.

Something along these lines should work:

class FILE(ctypes.structure):
    pass

FILE_p = ctypes.POINTER(FILE)

...but I haven't tested it.

You can also use a c_void_p.

> Also, is there any automated way to convert c struct and enum
> definitions to ctypes data types.

for structures:
http://code.activestate.com/recipes/576734-c-struct-decorator/?in=user-4170000

for functions:
http://code.activestate.com/recipes/576731-c-function-decorator/?in=user-4170000

Geremy Condra



More information about the Python-list mailing list