Folks,
first thanks for cffi, it seems a great project (I've used SWIG before). I'm just a newbie of cffi, so pardon if my question is silly. I've looked at the documentation and at this forum and I've not found an answer. The most similar one is
https://groups.google.com/forum/#!msg/python-cffi/E2QmmxyYPgA/MVXsV9k4A7UJ but it does not cover my use case.
I need to call a C function (from a library) whose argument is pointer to void. It's the C way of saying that the argument is "anything", and the library decided to do this way to implement just one function with the type specified as argument instead of a bunch of different identical functions The way I'd do this in C would be just create my stuff, possibly cast it (optional) and pass it to the func. Note that the func may change its value and I want to have that value back.
In C, is something like
#include <mylib.h>
double v = 2.34;
err = func(v, 1,DOUBLE_PRECISION);
// do something with err
printf("%d", v);
As you can see, the type and size must be passed explicitly (but that's not a big deal).
I'm trying to do this (in pypy, FWIW) with cffi as follows
ffi.cdef("""
typedef int datatype
int func(void*, int, datatype);
#define DOUBLE_PRECISION ...;
""")
C = ffi.verify("""#include <mylib.h>""")
value =
ffi.new("double *", 2.34);
v = ffi.cast("void *", value)
err = C.func(v, 1, C.DOUBLE_PRECISION)
Up to this point it seems to work. However, I cannot use the returned value (which is why I called func in the first place). If I do
print v[0]
I get
TypeError: cannot return a cdata 'void'
If I cast the appropriate datatype with:
value = ffi.cast("double *", v)
print value[0]
I get Segmentation fault.
What am I doing wrong or better what's the recommended way to use a C function with void * arguments?
Thanks
Davide