[Mono-dev] passing references to managed objects to unmanaged code
Jonathan Pryor
jonpryor at vt.edu
Tue Feb 6 06:44:24 EST 2007
On Tue, 2007-02-06 at 12:00 +0100, Andreas Lagemann wrote:
> I am trying to pass a reference to a managed object to unmanaged code
> where I would like to handle it like a MonoObject, so I could use the
> momo_embed API to call methods of the managed object from unmanaged code.
>
> What I have in mind is some kind of C#/C++ peership where each C#
> instance of a certain Object has a C++ peer object and the other way round.
You might look at Gtk#, Qt#, Kimono, or
http://www.mono-project.com/Dllimport#Avoiding_Marshaling if you want a
peering arrangement. This would work by having the C# types have an
IntPtr containing a pointer to the C++ object instance, and when you
P/Invoke your shared library you provide the C++ instance pointer.
> I tried something like this:
>
> C#:
> ...
> [DllImport("Simkernel",EntryPoint="SetCsharpPeer")]
However, if you want to use the Mono Embedding API, you can't use
[DllImport], you need to use
[MethodImplAttribute(MethodImplOptions.InternalCall)], e.g.:
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern void SetCsharpPeer (object o);
Doing this will require (1) that you embed Mono within your app, and (2)
call mono_add_internal_call ("YourType::SetCsharpPeer",
&CppType::SetCsharpPeer).
Note that you can't use C++ instance methods in this fashion, as Mono
won't know how to invoke C++ instance methods. Static methods should
work like this.
Alternatively, you can look into Jon Chamber's COM Interop support for
Mono, which allows you to use COM interfaces to have C# code directly
invoke C++ code, even on Linux.
- Jon
More information about the Mono-devel-list
mailing list