Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 738
Knio Avatar asked Jan 09 '10 05:01

Knio


1 Answers

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;
}
like image 104
Chris Avatar answered Oct 14 '22 02:10

Chris