ctypes help for "array of character pointers" as an output parameter

stalex shao.tu at gmail.com
Sat Jun 23 04:57:56 EDT 2007


I'm wrapping a C function exists in a shared library. Its prototype
looks like as follows

    int getFileNames(int aSize, char **names);

The documentation says that the asSize is the number of entries to be
returned and
names is output array of character pointers of at least aSize
elements. So, I defined
a function prototype in python, follow ctypes tutorial, and try to run
it. The following
code section shows what I did.

    _getNames = TheLib.getNames
    _getNames.restype = c_int
    _getNames.argtypes = [
        c_int,
        POINTER(c_char_p)
    ]

    def getNames():
        aSize = 1024
        names = (c_char_p * arraySize)()
        rc = _getNames(aSize, names)

After that, I make a call to getNames() in my python program and,
unfortunately, either TypeError
or Segmentation fault. Sigh...

I wonder that whether the _getNames prototype definition in my code
section is correct??
If it isn't, I guess so, how do I do a right one?
Another, whether the usage in getNames() is correct??
Appreciate for any reply or recommendation.




More information about the Python-list mailing list