473,666 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ 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 17235
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.r estype = c_ubyte_p

--
Gabriel Genellina

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

unsigned char *MagickGetImage Profile(MagickW and *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.MagickGetIm ageProfile.rest ype = c_char_p
result = dll.MagickGetIm ageProfile(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.MagickGetIm ageProfile.rest ype = 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.comescri bi�:
On 19 Aug., 20:40, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
>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 *MagickGetImage Profile(MagickW and *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.MagickGetIm ageProfile.rest ype = c_char_p
result = dll.MagickGetIm ageProfile(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(resul t, length)

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

--
Gabriel Genellina

Aug 23 '07 #5
On 23 Aug., 05:46, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
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(resul t, 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
4997
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 the status of a digital port of analog digital interface card. I got this function from Dask.h which came with the card. The relevant lines concerning this function are the following:
2
2323
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 : the class Id of objects owning attributes to
3
4227
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
1293
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 C module. At least on windows, I would have resource problems to compile the C module. So, ctypes is the perfect choice for me. But I am currently encountering a pattern inside the C library, that has to be used to free memory. Coding in C I...
0
1133
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 just pointlessly annoying. Hope this helps (and of course I trust you'll dispute if I'm nuts),
6
12814
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 I should declare the return type of the functions and read the fields. Any hint is appreciated. typedef struct { char *country_short; char *country_long;
0
2550
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
1333
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 when I try to print one of the args I did a pass byref on, no error out, nothing. admittedly I'm a newbie to ctypes and not much of a c programmer but I could sure use some help. my ctypes test code follows... from ctypes import * '''...
3
4522
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 gives me seg fault. But I can call same function from a C code for any number of times.
0
8449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8784
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.