ctypes issues involving a pointer to an array of strings

MRAB python at mrabarnett.plus.com
Tue Oct 20 22:28:34 CEST 2009

Nathaniel Hayes wrote:
> On Tue, Oct 20, 2009 at 2:08 PM, MRAB <python at mrabarnett.plus.com 
> <mailto:python at mrabarnett.plus.com>> wrote:
>     Nathaniel Hayes wrote:
>         On Tue, Oct 20, 2009 at 12:12 PM, MRAB
>         <python at mrabarnett.plus.com <mailto:python at mrabarnett.plus.com>
>         <mailto:python at mrabarnett.plus.com
>         <mailto:python at mrabarnett.plus.com>>> wrote:
>            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.
>         Well, the name of the device is DaqBoard2K, so thats where the
>         DaqB is coming from.  That means that the function worked, but I
>         can seem to get the information in a usable format.  I'm not
>         well versed in C or anything low level like that, so not really
>         sure what is going behind the scenes.  I have seen
>         (c_char_p*4)() would make an iterable ctypes array of 4 c_char_p
>         variables, but it seems like the address to the string has been
>         overwritten by the string itself?
>     What does the documentation of the C API actually say? What does the
>     parameter list of the C function look like?
>     -- 
>     http://mail.python.org/mailman/listinfo/python-list
> daqGetDeviceList(DaqDeviceListT *deviceList, DWORD *deviceCount);
> Is the prototype.  And the documentation:
> Function Usage
> The daqGetDeviceList function will return the device names in the 
> deviceList parameter for the number
> of devices returned by the deviceCount parameter. The deviceList entry 
> contains an array of device names
> each consisting of up to 64 characters. Each device name can then be 
> used with the daqOpen function to open the
> specific device. The DaqDeviceListT parameter must point to an 
> appropriately sized memory area which can
> hold all the names for all the configured devices before calling this 
> function. If it is not known how many devices
> are configured, then call the daqGetDeviceCount function before calling 
> this function.

It says "contains an array of device names", which to me sounds like you
need to give it a block of memory into which it will copy the names (max
64 characters/name). This will also explain why your test with Python 3
gave the characters of a name which you thought was a pointer to the

The definition of DaqDeviceListT will tell you exactly what you need (it
might be an array of 64 chars or an array of n such entries, for

The deviceCount parameter is a pointer too, so that suggests to me that
you tell it how many names you want and on returning it will have set
that to the number it has actually returned.

More information about the Python-list mailing list