ctypes and unsigned char*

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Aug 19 20:40:45 CEST 2007


On 19 ago, 14:44, "oliver.andr... at gmail.com"
<oliver.andr... at gmail.com> wrote:
> Hi,
>
> can anybody with ctypes experience tell me, how to handle a C function
> that returns an unsigned char*? Obviously it is not a restype of
> c_char_p.

Being signed or unsigned is not important here. But you have to
disambiguate "returns an unsigned char*"
- the function returns a string of bytes (unsigned char), null-
terminated.
- the function returns a pointer to a single byte.

In the first case, use a plain c_char_p - the individual "chars" are
already unsigned in Python (that is, ord(xxx[i]) is always positive)
In the second case, first define the pointer type:

c_ubyte_p = POINTER(c_ubyte)
your_function.restype = c_ubyte_p

--
Gabriel Genellina




More information about the Python-list mailing list