ctypes issues involving a pointer to an array of strings

Nathaniel Hayes nathaniel.hayes at maine.edu
Tue Oct 20 17:36:11 CEST 2009


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

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.

-- 
Nathaniel Christopher Hayes
Third Year Mechanical Engineering
Email: nathaniel.hayes at maine.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091020/4966fd53/attachment.html>


More information about the Python-list mailing list