Python ctypes and function pointers

This is related to my other question , but I felt like I should ask him a new question.

Basically, FLAC uses function pointers for callbacks and for making callbacks with ctypes, you use CFUNCTYPE

them to prototype them and then use a function prototype()

to create them.

The problem with this is that I figured I would create a callback function as such (I am not showing the structures I created, FLAC__Frame is a structure):

write_callback_prototype = CFUNCTYPE(c_int, c_void_p, 
                                     POINTER(FLAC__Frame), 
                                     POINTER(c_int32), v_void_p)
      

The problem I have is the implementation. FLAC__Frame is never instantiated by the programmer, it is only called from the initialization function and processing functions. I need to write a callback function, but the problem is that I don't know how to do this, so if anyone knows how I should do this, then it would be very helpful for us to help.

+1


a source to share


2 answers


According to the ctypes callback docs you can define a python function

def my_callback(a, p, frame, p1, p2)
    pass

      

and then create a pointer to a C function called like this:



callback = write_callback_prototype(my_callback)

      

This function pointer can then be passed to FLAC

+5


a source


The problem I have is the implementation. FLAC__Frame is never instantiated by the programmer, it is only called from the initialization function and processing functions. I need to write a callback function, but the problem is that I don't know how to do this, so if anyone knows how I should do this, then it would be very helpful for us to help.

In this case, just use:



import ctypes

class FLAC__Frame(ctypes.Structure):
    pass

      

and pretend it's already defined and don't care because you only need a pointer to it, which is basically a memory position.

+1


a source







All Articles