[Mono-list] Events in embedded Mono?

Robert Jordan robertj at gmx.net
Mon Nov 27 06:48:12 EST 2006

Hi Andreas,

Andreas Färber wrote:
> Hi Robert,
> Am 26.11.2006 um 21:59 schrieb Robert Jordan:
>>> How does one react to managed events in native code?
>>> One possibility I thought of was writing and loading managed code
>>> with a dispatcher method that p/invokes "__Internal".
>> This would work only with p/invoke compatible signatures.
>> If you need to handle signatures with object parameters,
>> an internal call is required (and it works perfectly, BTW).
> In that case, that would still mean writing a new managed assembly  
> just for this purpose.

Exactly. It could be the same assembly that helps you
switching to the NET_2_0 profile in mono_jit_init.

>>> * There is no mono_class_event_from_name function.
>>> (so I would have to iterate over all events myself - maybe a hint
>>> that this is not (widely) used yet?)
>> I'm using this method:
>> MonoEvent *
>> mono_class_get_event_from_name (MonoClass *clazz, const char *name)
>> {
>> 	gpointer iter;
>> 	MonoEvent *e;
>> 	while (clazz) {
>> 		iter = NULL;
>> 		while ((e = mono_class_get_events (clazz, &iter))) {
>> 			if (strcmp (name, mono_event_get_name (e)) == 0)
>> 				return e;
>> 		}
>> 		clazz = mono_class_get_parent (clazz);
>> 	}
>> 	return NULL;
>> }
> Then please add it to SVN! I checked SVN HEAD the weekend and it  
> wasn't there.

I didn't say it's in SVN. I copied the function verbatim from
my own code. Since I can't commit to the runtime core w/out review,
it will take some time to get this in.

>>> * There is a mono_create_ftnptr function for converting a function
>>> pointer into a form always usable by Mono.
>>> (but what is the expected function signature? is there a this-pointer
>>> preceding the arguments like in JNI?)
>> mono_create_ftnptr () doesn't exist, so you probably meant
>> mono_delegate_to_ftnptr (). This method is the low level
>> (and internal) counterpart of NET 2.0's
>> Marshal.GetFunctionPointerForDelegate ().
>> It works only for P/Invoke signatures, i.e. the signature
>> can't contain objects.
> I believe I copied the name from the online docs where it's described  
> to be necessary for Itanium. If it's not up-to-date someone should  
> remove it please or mark it appropriately.

The docs are up-to-date, but they seem to also contain some
of the internal functions.

Miguel, if you happen to read this: can the doc tool chain
flag functions marked with MONO_INTERNAL as such?

For now, the best way to assure that a function is public,
is to look at the *.h files of $monoprefix/includes/mono/.

>>> * mono_delegate_ctor has a gpointer addr parameter "pointer to native
>>> code" but also expects a MonoObject* "target object".
>>> (what's that supposed to be? can/should it be NULL? or is it not
>>> intended to be used that way?)
>> mono_delegate_ctor () is internal and, depending on the OS,
>> already unavailable since a couple of Mono releases.
> Again, I got this from the docs.
> So, summa summarum the only way is to write managed code that does an  
> internal call? No way to directly register such an internal method  
> for an event within my native code?

No way.

Actually you found the way how it could be done in the past, but it
was by no means (1) documented, (2) complete, (3) safe on all
architectures and it would require even more internal functions.

It's not worthwhile to try to live w/out a managed assembly.
Sooner or later you'll need it anyway, especially if you
want to handle all delegate types with exactly one internal call.


More information about the Mono-list mailing list