从C DLL到Python的回调 [英] C DLL to Python Callback
本文介绍了从C DLL到Python的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Visual C ++ DLL。我在DLL中导出了SetCallback(函数指针)。
我使用此函数从python2.7脚本设置回调函数。我按照Python文档中的说明进行操作。
I have a Visual C++ DLL. I have a SetCallback( function-pointer) exported in the DLL. I use this function to set a callback function from a python2.7 script. I follow what is given in the Python documentation.
from ctypes import *
def mypy_callback(number):
print str(number)
d = cdll.LoadLibrary(r"myfunctions.dll")
callback_type = CFUNCTYPE(None, c_int )
d.SetCallback(callback_type(mypy_callback))
在C代码中,我有
typedef void (*callback_function)(int);
void SetCallback(callback_function aCallback)
{
py_callback = aCallback;
}
当我从C DLL调用此函数时,如下所示:py_callback(999) ,python崩溃了。
我该怎么办?
When I call this function from C DLL, like so: py_callback(999), python just crashes. What could I be doing wrong?
推荐答案
以下回调间接寻址将解决此问题:
The following callback indirection will solve this problem:
d = cdll.LoadLibrary(r"myfunctions.dll")
callback_type = CFUNCTYPE(None, c_int )
callback = callback_type(mypy_callback)
d.SetCallback(callback)
这篇关于从C DLL到Python的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文