Python Cffi Pointer to Pointer for Out Argument

Python Cffi is C Foreign Function Interface that make a call to a function inside compiled dynamic library (dll/so) made easy than Ctypes does. The pointer to pointer argument will need some tricks and fundamental about pointers in C.

Solusi untuk kasus ini sama persis dengan masalah pada LuaJIT.

struct Foo { int dummy; }
int tryToAllocateFoo(Foo ** dest);

Foo *pFoo = NULL;
tryToAllocateFoo(&pFoo);
if (pFoo) printf("dummy == %d", pFoo->dummy);
from cffi import FFI

ffi = FFI()

ffi.cdef("""
struct Foo { int dummy; };
int tryToAllocateFoo(Foo ** dest);
""")

lib = ffi.dlopen("foo.dll")

pFoo = ffi.new('struct Foo *[1]')
pFoo[0] = ffi.NULL
ok = lib.tryToAllocateFoo(pFoo)

if pFoo[0] != ffi.NULL:
print('dummy == %d' % (pFoo[0].dummy,))