[Mono-dev] Embedded Mono IImplementation question..

Robert Jordan robertj at gmx.net
Sat Jul 22 09:32:09 EDT 2006


Hey,

Roger Davenport wrote:
> Can anyone explain the best way to pass delegates into C (for later
> callbacks) and later invoke them?
> 
> Currently, I it looks like I should create an extern static function (in
> C#) which is hooked into the mono class using mono_add_internal_call (in
> C).  I am guessing (from looking at the mono p/invoke functions) that it
> will be just a regular function pointer?

(1)
If you don't want to use Mono's p/invoke infrastructure
from managed code, you'd need an intermediary C wrapper from
which you invoke the managed delegate using

mono_get_delegate_invoke () and
mono_runtime_invoke ()

This is pretty slow.

(2)
If you're courageous you could use the non public

mono_delegate_to_ftnptr ()

declared in mono/metadata/marshal.h. Since this function is
not part of the public API, your app could cease working
in the future. Besides of this, it's pretty comfortable.

(3)
The NET 2.0 introduced

System.Runtime.InteropServices.GetFunctionPointerForDelegate

That's a managed wrapper for mono_delegate_to_ftnptr ().
You can call the method from C using mono_runtime_invoke ().


In all case you have to pin the managed delegate
using  mono_gchandle_new () and  mono_gchandle_free ().

> How will this work with compound delegates?

Do you mean multicast delegates? Yes.

Robert




More information about the Mono-devel-list mailing list