[Mono-list] passing MonoArray from c#

Robert Jordan robertj at gmx.net
Sun May 3 18:07:26 EDT 2009

>> Speaking of which, the moonlight code makes heavy use of delegates.. perhaps
>> because of performance? I tried to use a delegate to get the link data, but
>> the test

I don't know much about moolight, but yes, mono_runtime_invoke
is slower than a delegate call.

>>     retLinks = (*the_delegates.the_stringsfield_delegate)();
>> crashes immediately on entry. Can you tell me if i'm doing something wrong?

You cannot call a managed delegate directly from unmanaged code,
unless the delegate was created with


In turn, this method is creating a p/invoke wrapper which
can be pretty useless in embedded scenarios because it does
not allow objects in the signature.

A faster way to call managed method w/out having to
runtime_invoke them are managed thunks:

// your code that gets the MonoEmbed:getLinks method
desc = mono_method_desc_new("MonoEmbed:getLinks", TRUE);
method = mono_method_desc_search_in_image(desc,
	mono_assembly_get_image (assembly));
mono_method_desc_free (desc);

// Define a function pointer type for MonoEmbed:getLinks.
// Notice the "this" argument and MonoException**.
typedef MonoArray* (*GetLinksMethod)(MonoObject*, MonoException**);

// optain thunk with mono_method_get_unmanaged_thunk
GetLinksMethod getLinksThunk =
	(GetLinksMethod) mono_method_get_unmanaged_thunk (method);

// invoke the method
retLinks = getLinksThunk (instance, (MonoException**)&exc);

if (exc != NULL)
	abort ();

// enjoy ;)
printf ("length: %d\n", mono_array_length (retLinks));

I can't believe I've still not documented this stuff
properly :-( You can find some docs in metadata/object.c.


More information about the Mono-list mailing list