[C++-sig] In a C++ extension, how to use a C++ class exported in another extension

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sat May 14 03:32:21 CEST 2005

I guess the problem is that wxDC is not wrapped with Boost.Python, but with
SWIG. Therefore Boost.Python doesn't know how to extract the wxDC instance (or
a reference to the instance) from the Python object. I believe this can be
solved in a nice and general (via a custom from_python converter), but first
I'd experiment like this (untested):

  foo(boost::python::object wxDC_swig_object)


  def("foo", foo);

Pass your SWIG-wrapped wxDC instance to foo(). This should work in any case, I
hope. Once you get this working, develop the body of foo(). I'd look at the
SWIG generated code for a wxPython function that accepts a wxDC object as an
argument. SWIG uses PyArg_ParseTuple() to get a PyObject*; in foo() you want to
use wxDC_swig_object.ptr() to get the same pointer. You have to find out how
SWIG extracts the wxDC instance given the PyObject* and emulate this in the
body of foo(). From within foo() you can finally call your function expecting a
wxDC object.

Let us know if you get this far. The next step would be to rewrite foo() as a
custom converter. However, it is only worth it if you want to wrap a number of
functions with wxDC in the argument list. Otherwise the "thin wrapper" approach
above (foo() is the thin wrapper) is the final solution.

--- Hua Su <suh at mails.tsinghua.edu.cn> wrote:
> Hi,
> I'v learnt Boost.Python for two weeks and now encounter some problems :( 
>     In brief, I can't find the way to use a C++ class, which is exported in
> an
> existing extension, in another extension written by myself.
>     In detail, I'm writing a paint program with wxPython which is an
> extension for GUI library wxWidgets (wxWindows). I use wxPython to implement 
> GUI, and use C++ to implement paint operations. 
> ( In my application the paint operations is a bottleneck so I have to
> implement
>   is in C++ )
> When painting, I need to pass a pointer of DC from Python to my C++ code.
> The problem is, the class "wxDC" is a real C++ class writen in wxWidgets,
> and is exported in wxPython as Python class "DC", but I need use it in my
> code as C++ class "wxDC". I can't find support from Boost.Python for
> this problem. I have tried to pass pointer "void *" instead, but Boost.Python
> declares it does not support "void *" as argument or return type. 
>   Could someone give me some example? I work for this problem for days :(
> Thanks for your reply.
> regards,
> Clark

Yahoo! Mail Mobile 
Take Yahoo! Mail with you! Check email on your mobile phone. 

More information about the Cplusplus-sig mailing list