[Mono-dev] Embedding Mono and HandleRef...

Robert Jordan robertj at gmx.net
Sat Dec 30 20:57:05 EST 2006


Thomas Wiest wrote:
> Hey,
>     I'm using the Mono embedding API and I'm using HandleRef's instead 
> of IntPtr's (as per the instructions on the interop page).
> 
> 
> http://www.mono-project.com/Interop_with_Native_Libraries
> 
> 
> However, the HandleRef's seem to only be marshaled to IntPtr's if you're 
> using p/invoke's. I've attached a tar ball with two examples of what I'm 
> trying to do. Both examples can be compiled and run using "make run". 
> The Dll_Import example works flawlessly, marshaling the HandleRef into 
> an IntPtr. The mono_embed fails miserably because the HandleRef is still 
> a MonoObject (notice the memory address' don't line up as they do in the 
> Dll_Import example).
> 
>  From Robert Jordan's earlier e-mail it seems that the HandleRef is 
> intentionally not marshaled. So my question is, how do I get the 
> advantages of HandleRef's when using the embedding API? And if I just 
> switch back to IntPtr's, will my C# objects get collected before my 
> native code is done executing?

You don't need HandleRefs when using icalls and the embedded API.
Just declare the icalls as non-static and they will automatically
get the MonoObject* pointer of the managed object:

C#

     class Person
     {
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public extern void print_person();
     }


C:

void print_person(MonoObject* this)
{
}


Robert




More information about the Mono-devel-list mailing list