[Tutor] ctypes and arrays of pointers

Albert-Jan Roskam fomcl at yahoo.com
Thu Oct 13 21:25:52 CEST 2011


Hi,

I have a question about ctypes. I am trying to call a C function but I am not able to construct the arguments in ctypes. I need to construct two pointers. Each is a pointer to an array of pointers to character values. I did it like this (I tried numerous ways; this seemed the cleanest way):
>>> MAXSIZE = 10
>>> valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>>> valueArrayPtr
<class '__main__.LP_c_char_p_Array_10'>
>>> valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
>>> valueArrayPtrPtr
<class '__main__.LP_LP_c_char_p_Array_10'>
>>> 


But this yields an error: <type 'exceptions.TypeError'>: Don't know how to convert parameter ..

Any idea what I am doing wrong? Below is more complete code.
And once it works, can I simply use the ctypes as an iterable to retrieve the values?


Thanks in advance!
Albert-Jan


import ctypes

def getValueLabels(self, varName):

    MAXSIZE = 10

    # Pointer to array of pointers to values
    valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
    valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
    
    # Pointer to array of pointers to labels
    labelArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
    labelArrayPtrPtr = ctypes.POINTER(labelArrayPtr)

    # Pointer to number of values or labels
    numLabels = ctypes.c_int()
    numLabelsPtr = ctypes.byref(numLabels)
    
    # call C function with the following prototype:
    # int GetValueLabels(int handle, const char *varName, char ***values, char ***labels, int *numLabels)
    retcode = self.theLib.GetValueLabels(self.fh, varName, valueArrayPtrPtr, labelArrayPtrPtr, numLabelsPtr)
    # yields ArgumentError: argument 3: <type 'exceptions.TypeError'>: Don't know how to convert parameter 3


Description

This function gets the set of labeled values and associated labels for a short string
variable. The number of values is returned as *numLabels. Values are stored into an
array of *numLabels pointers, each pointing to a char string containing a nullterminated
value, and *values is set to point to the first element of the array. Each value
string is as long as the variable. The corresponding labels are structured as an array of
*numLabels pointers, each pointing to a char string containing a null-terminated label,
and *labels is set to point to the first element of the array. 
Parameter Description
handle Handle to the data file
varName Variable name
values Pointer to array of pointers to values
labels Pointer to array of pointers to labels
numLabels Pointer to number of values or labels


 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111013/344118ac/attachment.html>


More information about the Tutor mailing list