Some C libraries export function pointers such that the user of the library sets that function pointer to the address of their own function to implement a hook or callback.
In this example library liblibrary.so, how do I set library_hook to a Python function using ctypes?
library.h:
typedef int exported_function_t(char**, int); extern exported_function_t *library_hook;
This is tricky in ctypes because ctypes function pointers do not implement the
.valueproperty used to set other pointers. Instead, cast your callback function and the extern function pointer tovoid *with thec_void_pfunction. After setting the function pointer asvoid *as shown, C can call your Python function, and you can retrieve the function as a function pointer and call it with normal ctypes calls.