How to create a CFuncType in Python
I need to pass a callback function that is CF开发者_开发技巧uncType (ctypes.CFUNCTYPE or ctypes.PYFUNCTYPE...).
How can I cast a python function to CFuncType or how can I create a CFuncType function in python.
I forgot how awesome ctypes is:
Below is Copied from http://docs.python.org/library/ctypes.html
So our callback function receives pointers to integers, and must return an integer. First we create the type for the callback function:
CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
For the first implementation of the callback function, we simply print the arguments we get, and return 0 (incremental development ;-):
def py_cmp_func(a, b):
print "py_cmp_func", a, b
return 0
Create the C callable callback:
cmp_func = CMPFUNC(py_cmp_func)
精彩评论