[Tutor] ctypes and arrays of pointers

Albert-Jan Roskam fomcl at yahoo.com
Sat Oct 15 09:39:46 CEST 2011


 Hi,

Got it already. Here's some incomplete code that shows how it could be done:
# Pointer to array of pointers to labels 
labelsPtr = ctypes.pointer((ctypes.POINTER(ctypes.c_char_p) * MAX_ARRAY_SIZE)()) 
retcode = self.theLib.GetValueLabels(self.fh, varName, valuesPtr, labelsPtr, numLabelsPtr)
labels = [unicode(labelsPtr.contents[0][i], "utf-8") for i in range(numLabels.value)] 

Thought I might share this with you ;-)


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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


>________________________________
>From: Albert-Jan Roskam <fomcl at yahoo.com>
>To: Python Mailing List <tutor at python.org>
>Sent: Thursday, October 13, 2011 9:25 PM
>Subject: [Tutor] ctypes and arrays of pointers
>
>
>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?
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111015/f178c205/attachment-0001.html>


More information about the Tutor mailing list