Re: ctypes issues involving a pointer to an array of strings

Discussion in 'Python' started by MRAB, Oct 20, 2009.

  1. MRAB

    MRAB Guest

    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.
    >
     
    MRAB, Oct 20, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Podi

    ctypes pointer to pointer

    Podi, Sep 28, 2006, in forum: Python
    Replies:
    2
    Views:
    536
  2. Kavya
    Replies:
    2
    Views:
    397
  3. Replies:
    0
    Views:
    535
  4. MRAB
    Replies:
    0
    Views:
    768
  5. Grigory Petrov
    Replies:
    4
    Views:
    1,296
Loading...

Share This Page