开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜