问题描述
我需要传递一个回调函数,它是CFuncType(ctypes.CFUNCTYPE或ctypes.PYFUNCTYPE ...)。
I need to pass a callback function that is CFuncType (ctypes.CFUNCTYPE or ctypes.PYFUNCTYPE...).
如何将一个python函数转换为CFuncType或者如何在python中创建一个CFuncType函数。
How can I cast a python function to CFuncType or how can I create a CFuncType function in python.
推荐答案
我忘记了真棒ctypes是什么:
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))
对于回调函数的第一个实现,我们只打印获得的参数,并返回0增量开发; - ):
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
创建C callable回调:
Create the C callable callback:
cmp_func = CMPFUNC(py_cmp_func)
这篇关于如何在Python中创建CFuncType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!