ctypes question

djw dwelch at vcd.hp.com
Thu May 22 14:18:46 EDT 2003


Greetings,

I am trying to use ctypes to call the following function prototype:

int foo( ***bar ); // pointer to the character array pointer. 
        // callee allocates memory for array
        // caller use free() to deallocate memory when done

I have tried all sorts of incantations to make this work... here is my
current (totally non-working) attempt:

from ctypes import *
libxxx = cdll.LoadLibrary('/usr/lib/libxxx.so')
foo = libxxx.foo
foo.restype = c_int
a = POINTER(POINTER(c_char_p))
i = foo(byref(a))

>>> TypeError: expected CData instance

I've also seen errors like:

>>> TypeError: _type_ must have storage info

and 

>>> TypeError: while constructing argument 1:
>>> Don't know how to convert parameter 1

...all understandable except the "_type_" one - not sure what that means.

I've also tried pointer() instead of POINTER() [not sure what the difference
is]. Also tried a without the byref().  I'm probably way off the mark here!

I also don't quite get how to free() the memory from Python of the array
after libxxx.foo() has allocated the memory - seems like I'd have to call
free() in libc with a "bare" C pointer?

Any suggestions are appreciated!

Thanks,

Don








More information about the Python-list mailing list