I have a DLL in c and meant for windows platforms which has a structure similar to the following:
C Structure
typedef struct some_struct {
int (_stdcall *function)(int arg1, int arg2);
...
}SOME_STRUCT;
I've defined a python ctypes structure to mimic this with the following
Python Structure
class SOME_STRUCT(Structure):
_fields_ = [('function', POINTER(CFUNCTYPE(c_int, c_int, c_int))), ...]
The point of this structure in the C code is to register a callback function that gets executed upon certain triggers in its own thread. What I want to be able to do, if possible, is set that callback to be a Python function such that when the function in the C structure gets called from the C code, it is the python function that gets executed.
What I've got in python to try and accomplish this (which doesn't work) is the following:
def func(arg1,arg2):
print('I was called!')
return 0
struct = SOME_STRUCT()
prototype = CFUNCTYPE(c_int, c_int, c_int)
struct.function = byref(prototype(func))
The specific error I get (that may not be my only issue) is that it complains that struct.function was expecting a LP_CFunctionType instance but got a CArgObject instance. How can I do what I'm trying to do?
byrefas you are doing. Does it work if you remove the byref call and just dostruct.function = prototype(func)? - Paul Corneliusbyrefor something similar since I'm trying to assign a pointer to a function rather than a function itself (which the example is doing). - zephyr