473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ctypes and unsigned char*

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.

Best regards,
Oliver

Aug 19 '07 #1
5 17157
ol************@gmail.com schrieb:
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.
From the docs:

c_ubyte

Represents the C unsigned char datatype, it interprets the value as
small integer. The constructor accepts an optional integer initializer;
no overflow checking is done.

Diez
Aug 19 '07 #2
On 19 ago, 14:44, "oliver.andr...@gmail.com"
<oliver.andr...@gmail.comwrote:
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

Aug 19 '07 #3
On 19 Aug., 20:40, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
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
I will have to look into that. My function I want to wrap has the
following signature

unsigned char *MagickGetImageProfile(MagickWand *wand,const char
*name,
size_t *length)

Well, the first argument and the second are straight forward, and work
as expected. The last argument also works and it should take up the
length of the string (unsigned char *). If I now do

dll.MagickGetImageProfile.restype = c_char_p
result = dll.MagickGetImageProfile(wand, "8bim", byref(length))

result is either an empty string or a 3 bytes long string, while
length containes the correct length of 1086 bytes for example. I also
tried

dll.MagickGetImageProfile.restype = POINTER(c_ubyte)

with the same result. But maybe I should do some further debugging....

Best regards,
Oliver

Aug 20 '07 #4
En Mon, 20 Aug 2007 08:30:50 -0300, ol************@gmail.com
<ol************@gmail.comescribi�:
On 19 Aug., 20:40, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
>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

I will have to look into that. My function I want to wrap has the
following signature

unsigned char *MagickGetImageProfile(MagickWand *wand,const char
*name,
size_t *length)

Well, the first argument and the second are straight forward, and work
as expected. The last argument also works and it should take up the
length of the string (unsigned char *). If I now do
So, if I understand correctly, after the call, *length will contain the
length of the buffer pointed by the function result.
dll.MagickGetImageProfile.restype = c_char_p
result = dll.MagickGetImageProfile(wand, "8bim", byref(length))

result is either an empty string or a 3 bytes long string, while
length containes the correct length of 1086 bytes for example. I also
tried
If the result can contain nul bytes, you have to specify its size
explicitely (else it will be truncated at the first zero). Use the
string_at utility function:

result_str = string_at(result, length)

(That should work fine, I presume...)

--
Gabriel Genellina

Aug 23 '07 #5
On 23 Aug., 05:46, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
If the result can contain nul bytes, you have to specify its size
explicitely (else it will be truncated at the first zero). Use the
string_at utility function:

result_str = string_at(result, length)

(That should work fine, I presume...)
Thank a lot, that does exactly what I expected it to do.

Best regards,
Oliver

Aug 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rubbishemail | last post by:
Hello, i've got a problem with pointers in the following function which i want to use: I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value) The function is supposed to read out...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
3
by: p.lavarre | last post by:
Subject: Python CTypes translation of (pv != NULL) And so then the next related Faq is: Q: How should I test for ((void *) -1)? A: (pv == 0xffffFFFF) works often.
0
by: Oliver Andrich | last post by:
Hi everybody, I am currently playing around with ctypes and a C library (libWand from ImageMagick), and as I want to easily deploy it on Mac, Linux and Windows, I prefer a ctypes solution over a...
0
by: p.lavarre | last post by:
Now at http://pyfaq.infogami.com/suggest we have: FAQ: How do I declare that a CTypes function returns unsigned char? A: c_uchar = ctypes.c_ubyte This irregularity actually is doc'ed, it's...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
0
by: Egor Zindy | last post by:
Egor Zindy wrote: #!/usr/bin/env python """ A generic chipid library based on ctypes This module handles most of the functions in FTChipID.dll
2
by: Sells, Fred | last post by:
Diez wrote... You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters that the c api uses. my ctypes code is below. It just quits running...
3
by: patelss23 | last post by:
Hello All, I am dealing with this weird bug. I have a function in C and I have written python bindings for it using ctypes. I can call this function for couple of times and then suddenly it...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.