尝试创建一个需要在调用 C 回调时调用的 python 回调 来自 Windows 环境中的 dll。请查看下面的代码以了解该问题。
from ctypes import *
#---------qsort Callback-------------#
IntArray5 = c_int * 5
ia = IntArray5(5,1,7,33,99)
libc = cdll.msvcrt
qsort = libc.qsort
qsort.restype = None
CMPFUNC = CFUNCTYPE(c_int,POINTER(c_int),POINTER(c_int) )
test = 0
def py_cmp_func(a,b):
#print 'py_cmp_func:',a[0],b[0]
global test
test = 10000
return a[0]-b[0]
cmp_func = CMPFUNC(py_cmp_func)
qsort(ia, len(ia), sizeof(c_int), cmp_func)
print "global test=",test
for item in ia : print item
#----------Load DLL & Connect ------------#
gobiDLL = WinDLL("C:\LMS\QCWWAN2k.dll")
print 'Output of connect : ',gobiDLL.QCWWANConnect()
#----------SetByteTotalsCallback----------#
tx = POINTER(c_ulonglong)
rx = POINTER(c_ulonglong)
proto_callback = WINFUNCTYPE(c_void_p,tx,rx)
gtx = grx = 0 # Used to copy the response in the py_callback
def py_callback(t,r):
sleep(10)
print 'python callback ...'
print "tx=",t,"rx=",r
global gtx,grx
gtx = 5000 # gtx = t
grx = 2000 # grx = r
#return 0
callback = proto_callback(py_callback)
gobiDLL.SetByteTotalsCallback.restype = c_ulong
gobiDLL.SetByteTotalsCallback.argtypes = [proto_callback,c_byte]
print "SetByteTotalsCallback = ",gobiDLL.SetByteTotalsCallback(callback, c_byte(256))
print "gtx = ",gtx
print "grx = ",grx
DLL 记录了 SetByteTotalsCallback() 方法的原型(prototype)和回调,如下所示。
原型(prototype):
ULONG QCWWANAPI2K SetSessionStateCallback( tFNSessionState pCallback );
回调:
void ByteTotalsCallback( ULONGLONG txTotalBytes, ULONGLONG rxTotalBytes );
输出:
>>>
global test= 10000
1
5
7
33
99
Output of connect : 0
SetByteTotalsCallback = 0
gtx = 0
grx = 0
>>>>
当前的问题是整个程序被正确调用, 但 python 回调根本没有被调用。程序以 0 状态退出 gobiDLL.SetByteTotalsCallback(callback, c_byte(256))方法,但是callback()方法写成 在python中,调用过程中不会调用。
您能指出什么可以帮助进入 python 回调吗? 另一个示例 qsort() 方法将指针传递给 python 函数指针。 无法在这里找到问题的根本原因。
TIA,
安东尼
最佳答案
你不能。 C/C++ 函数无法直接访问 Python 函数 - 该函数原型(prototype)可能需要一个指向 C 的指针。Python 将向其传递一个指向该特定函数的内部数据结构的指针。
现在是构建 Python 的 C 扩展来包装该 DLL 并将其公开给 Python 的时候了。您要做的本质上是让 C 回调调用 Python 回调,因为这是可以完成的。更清楚地说,您想要实现的是:
| This side is C land i.e. "real" addresses
|
Python objects --> C extension -- register callback with --> DLL
| |
in the python | Calls callback
| |
interpreter <-------------- Callback in C extension <-------
|
以下是关于构建从 C 调用 python 函数的非常快速的说明。您需要使用用于构建 Python 发行版的 MSVC(或替代工具)来构建此代码;使用 depends.exe
找出它所链接的 msvcXX.dll
。
全局状态通常被认为是不好的,但为了简单起见,我使用的是:
static PyObject* pyfunc_event_handler = NULL;
static PyObject* pyfunc_event_args = NULL;
然后,我添加了一个设置处理函数,以使设置回调的过程变得更容易。但是,您不需要这样做,您只需要
static PyObject* set_event_handler(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
下一行是重要的 - 我允许传递两个 python 对象(将 O
参数传递给 PyArg_ParseTuple
。一个对象包含函数,另一个包含其参数。
if (PyArg_ParseTuple(args, "OO", &temp, &pyfunc_event_args)) {
if (!PyCallable_Check(temp)) {
PyErr_SetString(PyExc_TypeError, "parameter must be a function");
return NULL;
}
整理引用资料。 Python 需要你这样做。
Py_XINCREF(temp); /* Add a reference to new func */
Py_XDECREF(pyfunc_event_handler); /* Dispose of previous callback */
pyfunc_event_handler = temp; /* Remember new callback */
/* Boilerplate to return "None" */
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}
然后您可以在其他地方调用它:
PyObject* arglist = Py_BuildValue("(O)", pyfunc_event_args);
pyobjresult = PyObject_CallObject(pyfunc_event_handler, arglist);
Py_DECREF(arglist);
不要忘记DECREF
,您需要Python来GC arglist。
在 python 中,使用它非常简单:
set_event_handler(func, some_tuple)
其中 func 具有如下匹配参数:
def func(obj):
/* handle obj */
您可能想阅读的内容:
- LoadLibrary (从 C 加载 DLL)。
- GetProcAddress (找到要调用的函数)。
- Extending Python with C or C++来自 Python 文档。
关于C 回调的 Python 包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6412828/