views:

11

answers:

0

I understand I can set a global function pointer inside a sharef library to point to a python function as follows.

from ctypes import *

liblibrary = cdll.LoadLibrary('liblibrary.so')

def py_library_hook(strings, n):
    return 0

# First argument to CFUNCTYPE is the return type:
LIBRARY_HOOK_FUNC = CFUNCTYPE(c_int, POINTER(c_char_p), c_int)
hook = LIBRARY_HOOK_FUNC(py_library_Hook)
ptr = c_void_p.in_dll(liblibrary, 'library_hook')
ptr.value = cast(hook, c_void_p).value

Question: I have a WEAKREF stub function defined in shared library, which is expected to be overriden by an application including it. I am using python ctypes load that shared library. How do I override the function in the shared library with a python function?