[Mono-dev] storing references to mono-objects in C++ and passing them back to C#

Jonathan Chambers joncham at gmail.com
Fri Mar 16 09:19:16 EDT 2007

Hi Andreas,
      It sounds like what you are doing is embedding mono. Did you see this
page on the wiki (http://www.mono-project.com/Embedding_Mono)? To answer
your #2 question, I think you want your menthod to be an internal call,
rather than a pinvoke. Internal calls do no marshalling, so you will be able
to return an object directly. See the samples that ship with mono, or this
part of the link above (

So, quickly it will look something like...


public static extern object getPeerObjectOf(HandleRef jarg1);

And your C++ will almost stay the same; I'm not sure but you may need to
adjust your use of HandleRef since it will no longer be marshalled like a
pinvoke, so you'll be getting a real HandleRef structure.

Also, don't forget if you make the method an instance method rather than
static, the C code needs "MonoObject* this_ptr" as the first argument.


On 3/16/07, Andreas Lagemann <ae at informatik.tu-cottbus.de> wrote:
> Hash: SHA1
> Hi all,
> I want to do the following in a program which consists of
> interacting parts of native C++(not managed) code and C# on mono
> where the mono runtime is started from the C++ code and C# code
> calls back to C++:
> 1. I want to store a reference to an already existing mono-object on
> the C++ side. I currently do it this way:
>     typedef MonoObject* (*getCsharpPeerFn)();
>     void setCsharpPeerCallback(getCsharpPeerFn fnptr)
>     {
>         getCsharpPeer = fnptr;
>         csharpPeer = getCsharpPeer();
>         csharpPeerHandle = mono_gchandle_new(csharpPeer, TRUE);
>     }
> where the setCsharpPeerCallback method is called from C#-code via
> P/Invoke.
> That works fine so far. But additionally I would like to:
> 2. retrieve the stored reference from within managed code by calling
> the unmanaged side. I tried something like that:
> C#:
>   [DllImport("Simkernel",EntryPoint="getPeerObjectOf")]
>   public static extern object getPeerObjectOf(HandleRef jarg1);
> C++:
>    MonoObject* getPeerObjectOf(SomeCommonClassBase* obj) {
>         MyClass* myObj = dynamic_cast<MyClass*>(obj);
>         return myObj ? myObj->getMyPeerObject() : NULL;
>    }
> When I call getPeerObjectOf from C# I get an runtime Error, stating
> that marshalling of object is not implemented.
> Is there another way to do what I want?
> Is marshalling of type object supported in more recent mono versions
> (I'm using version
> Please help, I'm really becoming desperate about this.
> Thank you for reading and best regards,
> Andreas
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> iD8DBQFF+pVANfV8qlW7iaERAof6AJ42HTgQqOnJSRDCaS7/U+7ZSwliqQCfZ0kd
> c9orZDWbGGkhnxi8rGQ/vx8=
> =Ck74
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070316/8a979bbc/attachment.html 

More information about the Mono-devel-list mailing list