I'm trying to wrap a C library in Python using ctypes. A function call requires a callback function which I implemented using the documentation. The problem is that the callback function is expecting custom objects from the library. Here is the code in C I'm trying to copy in python
void outputCallback(const A* a, void* b) {
//
}
a = function1(0, 0, outputCallback, 0, 0)
The structure definition for A in the header file is:
typedef struct A
{
const unsigned char* a1;
unsigned int a2;
} A;
and my attempt at the Python equivalent.
class A(Structure):
_fields_ = [
("a1", ?, ?),
("a2", c_int, 16)]
class Callback():
def outputCallback(self, a):
print a.a2
return 1
cb = Callback()
CMPFUNC = CFUNCTYPE(c_int, POINTER(A))
cb.cmp_func = CMPFUNC(cb.outputCallback)
cdll.LoadLibrary("library.so")
libc = CDLL("library.so")
a = libc.function1(0, 0, cb.cmp_func, 0, 0)
The reason I wrapped the callback in a class is because of this post. Basically it was my attempt at keeping this callback from being garbage collected.
Thanks for any help you can offer.