Python ctypes callback function to SWIG

你离开我真会死。 提交于 2019-12-12 11:18:00

问题


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.

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.

What can I do to pass my callback to SWIG so that it understands it?


回答1:


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:

// 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;
}


来源:https://stackoverflow.com/questions/2032470/python-ctypes-callback-function-to-swig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!