views:

325

answers:

1

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  A: 

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