Problem with char ** as a output function parameter

22 views
Skip to first unread message

Jacek Bzdak

unread,
Feb 9, 2015, 6:49:59 AM2/9/15
to pytho...@googlegroups.com
Hi,

I'm wrapping a library that contains function that has an API similar to

void GetParameter(int param_id, void** param_text, unsigned int text_length);

and I'm supposed to call it (using C code):

char[8] result;
GetParam(1, &result, 8) //This fills result with some data
printf(result);

I'm having big problems recreating this in cffi code. Maybe I'm missing something obvious.

I have tried (among other things):

data = ffi.new("char[]", 8)
data[0:8] = b' '*8
ffi.addressof(data) # TypeError: expected a struct or union ctype

Any pointers?

JB

Armin Rigo

unread,
Feb 9, 2015, 11:28:39 AM2/9/15
to pytho...@googlegroups.com
Hi Jacek,

On 9 February 2015 at 12:49, Jacek Bzdak <jbz...@gmail.com> wrote:
> char[8] result;
> GetParam(1, &result, 8) //This fills result with some data
> printf(result);

Hum, are you very sure that this C code is correct? The obvious typos
--- there are 4 of them on 3 lines --- make me think that you didn't
try to execute such C code...

What does GetParameter() do with its "void **" argument? Shouldn't
the C code be rather like this:

void *result;
GetParameter(1, &result, 8); // returns a "void *" by writing it to result
puts((char *)result); // print it by assuming a pointer to
// a null-terminated string

If it is, then the corresponding cffi is:

p_result = ffi.new("void *[1]")
GetParameter(1, p_result, 8)
print ffi.string(ffi.cast("char *", p_result[0]))


A bientôt,

Armin.

mgr inż. Jacek Bzdak

unread,
Feb 9, 2015, 1:01:38 PM2/9/15
to pytho...@googlegroups.com
Hi Armin,

It was a pseudocode, actual runnable code is like this more complicated (and I wouldn't ever been able to provide runnable example since library I working with is proprietary).

Anyways thanks both for your help, and for the cffi --- which is best tool for working with C code I worked with :)!

JB



--
-- python-cffi: To unsubscribe from this group, send email to python-cffi...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/python-cffi?hl=en
---
You received this message because you are subscribed to the Google Groups "python-cffi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-cffi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Armin Rigo

unread,
Feb 10, 2015, 12:08:31 PM2/10/15
to pytho...@googlegroups.com
Hi,

On 9 February 2015 at 19:01, mgr inż. Jacek Bzdak <jbz...@gmail.com> wrote:
> It was a pseudocode, actual runnable code is like this more complicated

The point is that you have to start with "how would I call this in C"
and then translate to CFFI. Your example was not pseudo-code, it was
downright wrong as far as I can tell...


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages