ctypes issues involving a pointer to an array of strings

MRAB python at mrabarnett.plus.com
Tue Oct 20 12:12:03 EDT 2009


Nathaniel Hayes wrote:
> I am working on creating a python API for a dll for a daqboard we have 
> here.  I have the documentation for the library, as well as the C, 
> Delphi and VB api.  I have decided to use ctypes for the project, and 
> most is working out alright.  However I have run in to considerable 
> headaches when dealing with pointers to arrays.  The function I am first 
> working with, requires a pointer to an array of strings that will 
> contain the names of all the devices installed.  Here is a snippet of 
> the code I have so far:
> 
> def GetDeviceList():
>     """Returns a list of currently configured device names"""
> 
>     devices = []
>     count = GetDeviceCount()
>     countpnt = ct.pointer(ct.c_int(count))
>     devlist = (ct.c_char_p * count)()
>        
>     daq.daqGetDeviceList(devlist, countpnt)
>    
>     for i in devlist:
>         devices.append(i)
>        
>     return devices
> 
> Using Python 2.6.3 the IDLE goes into an infinite loop of, from what I 
> can gather, Tkinter errors, and Im unable to kill the command to 
> actually read all of the errors.
> 
> When using Python 3.1.1, I get this error:
> 
> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit 
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>  >>> import daqX
>  >>> daqX.GetDeviceList()
> Traceback (most recent call last):
>   File "<pyshell#1>", line 1, in <module>
>     daqX.GetDeviceList()
>   File "C:\Python31\daqX.py", line 54, in GetDeviceList
>     for i in devlist:
> ValueError: invalid string pointer 0x42716144
>  >>>
> 
The digits in that pointer value look suspiciously like the character
codes of a string rather than an actual address:

 >>> "\x42\x71\x61\x44"
'BqaD'

It looks like the first 4 characters of a string starting 'DaqB' are
being used as a string pointer on a little-endian platform.

> I am really at a loss here, so any insight at all would be great.  
> Something odd though, is that when I pass just c_char_p instead of an 
> array of c_char_p, I receive the one device I have installed on this 
> computer, and it works great.  However, when I make an array of just one 
> item, I get these odd errors.
> 



More information about the Python-list mailing list