[Mono-dev] Mixed Mode Assemblies

Robert Jordan robertj at gmx.net
Wed Jul 20 11:05:20 EDT 2011

Hi Tom!

On 20.07.2011 15:14, Tom Spink wrote:
> Hi Guys - if you're still watching this thread,
> So I may have done something brilliant - or something terrible.  I don't
> quite know yet - I need your feedback to see if I'm heading along the right
> lines.
> What I've done is to create a tool that creates a shared library with an
> embedded PE assembly inside.  The shared library exports symbols that
> pertain to methods of the assembly, and when you link against this shared
> library, in a native application, you can then call those methods, without
> having to bother with using Mono's embedding API.
> Essentially, a bunch of method stubs are created that when called ensure the
> runtime is initialised, the assembly loaded (from inside the shared library)
> and then locate and call the appropriate method.  After the runtime has been
> loaded, the first few bytes of the method are overwritten with NOPs to
> ensure the runtime isn't loaded twice.
> The symbols exposed take the form of 'Namespace_Class_MethodName', which
> just works for static methods, but to call instance methods, you construct
> the class with a call to a 'new_Namespace_Class' function, then pass in the
> object instance as the first member.
> At the moment, the Makefile is more automated than the code generator, but
> if this seems like a worthwhile direction to head in, let me know and you
> can give me some feedback on my (rudimentary) implementation.
> Anyone interested at all? Or am I barking mad^H^H^Hup the wrong tree?

Nice :) Not exactly "Mixed Mode Assemblies" though, because the
really interesting part (calling unmanaged code w/out having to
go through p/invoke) remains the same pain.

Out of curiosity: How are you handling more than one of those
"mixed assemblies" in the same process? Is there a function
inside libmono that can be used to detect an already initialized

On the generated stubs: are you using thunks or mono_runtime_invoke?
Are you rewriting the signature of the stubs to be HRESULT-like
(to be able to catch/detect managed exceptions)?


More information about the Mono-devel-list mailing list