[英]Python ctypes callback function to SWIG
I have a SWIG C++ function that expects a function pointer (WNDPROC), and want to give it a Python function that has been wrapped by ctypes.WINFUNCTYPE. 我有一个SWIG C ++函数,需要一个函数指针(WNDPROC),并希望给它一个由ctypes.WINFUNCTYPE包装的Python函数。
It seems to me that this should be compatible, but SWIG's type checking throws an exception because it doesn't know that the ctypes.WINFUNCTYPE type is acctually a WNDPROC. 在我看来,这应该兼容,但SWIG的类型检查会引发异常,因为它不知道ctypes.WINFUNCTYPE类型实际上是WNDPROC。
What can I do to pass my callback to SWIG so that it understands it? 我可以做些什么来将我的回调传递给SWIG,以便它理解它?
I don't have a windows machine to really check this, but I think you need to create a typemap to tell swig how to convert the PyObject wrapper to a WNDPROC: 我没有Windows机器来真正检查这个,但我认为你需要创建一个typemap来告诉swig如何将PyObject包装器转换为WNDPROC:
// assuming the wrapped object has an attribute "pointer" which contains
// the numerical address of the WNDPROC
%typemap(in) WNDPROC {
PyObject * addrobj = PyObject_GetAttrString($input, "pointer");
void * ptr = PyLong_AsVoidPt(addrobj);
$1 = (WNDPROC)ptr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.