如何在Python中创建CFuncType

man*_*n54 7 python ctypes callback

我需要传递一个CFuncType(ctypes.CFUNCTYPE或ctypes.PYFUNCTYPE ...)的回调函数.

如何将python函数转换为CFuncType或如何在python中创建CFuncType函数.

Ada*_*ent 9

我忘记了很棒的ctypes:

以下是从http://docs.python.org/library/ctypes.html复制的

所以我们的回调函数接收指向整数的指针,并且必须返回一个整数.首先,我们为回调函数创建类型:

CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
Run Code Online (Sandbox Code Playgroud)

对于回调函数的第一个实现,我们只是打印我们得到的参数,并返回0(增量开发;-):

 def py_cmp_func(a, b):
     print "py_cmp_func", a, b
     return 0
Run Code Online (Sandbox Code Playgroud)

创建C可调用回调:

cmp_func = CMPFUNC(py_cmp_func)
Run Code Online (Sandbox Code Playgroud)

  • 从[this](https://gitorious.org/telldus/telldus/commit/1108f3fad26f744c2418d22a547a17669522ce2e)看来,应该使用“无”。 (2认同)