[Mono-dev] Embedding-API & unmanaged wrappers

Robert Jordan robertj at gmx.net
Sat Mar 11 08:44:01 EST 2006


Hi,

I have to handle events fired by the managed world with
an unmanaged C event handler. To do this, I'm creating a
delegate this way:

/* unmanaged sig of System.EventHandler */
typedef void EventHandler(MonoObject *context,
	MonoObject *sender, MonoObject *args);

MonoDelegate*
create_EventHandler_delegate (MonoClass *clazz, EventHandler *ftnptr
	MonoObject *context)
{
	static gpointer compiled_wrapper = NULL;
	MonoMethod *invoke = mono_get_delegate_invoke (clazz);
	MonoMethodSignature *sig = mono_method_signature (invoke);
	MonoDelegate *delegate = (MonoDelegate *)
   		mono_object_new (mono_domain_get (), clazz);

	/* create & compile a wrapper */
	if (!compiled_wrapper) {
		MonoMethod *wrapper;
		int saved_pinvoke = sig->pinvoke;

		sig->pinvoke = 1;
		wrapper = mono_marshal_get_icall_wrapper (sig,
			"native_EventHandler_wrapper",
			(gconstpointer) ftnptr);
                 sig->pinvoke = saved_pinvoke;

                 compiled_wrapper = mono_compile_method (wrapper);
	}

	/* construct the delegate */
         mono_delegate_ctor ((MonoObject*) delegate, context,
		compiled_wrapper);

	return delegate;
}


Now the question: is mono_marshal_get_icall_wrapper () the only way
to create a plain (w/out marshaling) wrapper for an unmanaged ftnptr?
I'm worried about the ugly "sig->pinvoke = 1" hack and about
the usage of 2 internal functions.

Regards,
Robert





More information about the Mono-devel-list mailing list