I have structs in a C library that are like this. The function pointers in DataFn point to static functions.
.h
struct Data {
int i;
int *array;
};
typedef struct {
bool (* const fn1) (struct Data*, const char *source);
....
} DataFn;
extern DataFn const DATAFUNC
Using objdump, the table only contains DATAFUNC and a few other things from gcc.
This is fine in C where calling fn1 would go like DATAFUNC.fn1(..., ...), but how would something like this be wrapped around so fn1 can be called in python w/ ctypes?
Example python
libc = ctypes.cdll.LoadLibrary("./data.so")
print(libc.DATAFUNC)
results in
<_FuncPtr object at 0x6ffffcd7430>
[Python 3.Docs]: ctypes - A foreign function library for Python contains everything required to solve this problem.
I believe that the main piece missing, was the in_dll method of a ctypes type (Accessing values exported from dll section).
Other than that, in order to work with C data, you need to let Python know of the data format. That applies to:
ctypes.Structure
ctypes.CFUNCTYPE
I prepared a simplified example that illustrates the above. Note that I didn't do any error handling (checking for NULLs (which you should)), to keep things simple.
c.h:
c.c:
code00.py:
Output: