'CFFI Pass callback as parameter to another function
Here is an explanation of my misunderstanding:
I have a class named Codex:
class Codex:
ffi = FFI()
def __init__(self):
self.ffi = FFI()
self.ClibDec = self.ffi.dlopen("...")
self.ffi.cdef("""int CRD_Set(int handle, int property, void *value);
int CRD_Get(int handle, int property, void *value);
""")
def funct1(self):
handle = self.ClibDec."something"
self.ClibDec.CRD_Set(handle, 0, funct2)
@ffi.callback("int(*)(int, int)")
def funct2(self, handle):
status = 0
self.ClibDec.CRD_Get(handle, 420, status)
# do something
return 0
My question is how can I call funct2 inside self.ClibDec.CRD_Set in my funct1 ?
I check several similar issues and the doc: https://cffi.readthedocs.io/en/latest/using.html#callbacks-old-style
But I still not understand the usage of callback (old style)
Solution 1:[1]
Maybe you are looking for code like this? This would be the solution closest to what you're trying to write.
class MyClass:
def funct1(self):
handle = self.ClibDec."something"
# make a cdata function pointer object that will call
# back self.funct2(handle)
cfnptr = ffi.callback("int(*)(int)", self.funct2)
self.ClibDec.CRD_Set(handle, 0, cfnptr)
# the cfnptr pointer must not be called after
# the variable 'cfnptr' goes out of scope,
# which occurs when funct1() returns here.
def funct2(self, handle):
...
return 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Armin Rigo |
