[Mono-list] Embedding Mono : Classes

Chris Howie cdhowie at gmail.com
Wed Jul 15 12:36:17 EDT 2009

On Wed, Jul 15, 2009 at 11:58 AM, Alex
Kuster<x.passion.and.the.opera.x at gmail.com> wrote:
> but, there is no doc about exposing a C++ class (or, at least, emulate
> it) to be used on the C# side ..

There is no automagic way to do this, because every C++ compiler has a
different name-mangling mechanism for class methods.  You might be
able to use SWIG to help out, though.

Basically, what you need to do is construct a managed class with the
methods you want to expose.  It will probably need to contain an
IntPtr member, which will contain a pointer to the C++ object.  Note
that the managed class *will not* be able to detect when this pointer
is freed.  This means that, for each class you want to wrap this way,
the C++ class should also keep track of the MonoObject associated with
your managed wrapper, and notify it during destruction, probably by
setting the IntPtr to IntPtr.Zero.

Then you will need to write a bunch of wrapper functions declared as
extern "C" in your runtime.  You will need to ensure that they export
their symbols, and then craft [DllImport("__Internal")] methods on the
wrapper class that your public method calls can delegate to.  This is
how the calls will find their way back into unmanaged land.  SWIG will
probably be the most help here.

Chris Howie

More information about the Mono-list mailing list