Python ctypes callback function for SWIG

I have a C ++ SWIG function that expects a function pointer (WNDPROC) and wants to give it a Python function that was wrapped by ctypes.WINFUNCTYPE.

It seems to me that this should be compatible, but checking for the SWIG type throws an exception, because it does not know that the ctypes.WINFUNCTYPE type is actually WNDPROC.

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

+5
source share
1 answer

I don’t have a window machine to really test this, but I think you need to create a sample map to tell swig how to convert the PyObject wrapper to 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;
}
+3
source

All Articles