473,388 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 17142
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...

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.