[Mono-dev] Mixed Mode Assemblies

Ivo Smits Ivo at UFO-Net.nl
Thu Jul 21 18:56:13 EDT 2011

Op 20-7-2011 15:14, Tom Spink schreef:
> 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.
That doesn't give the full power of Mixed Mode assemblies, but I think 
it's definitely an interesting approach with many potential uses. 
Especially, I think, when replacing existing libraries or building 
plugins or extensions for existing software.

> 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.
I would rather skip the rewriting, and just start with a simple 'if 
(!initialized) initialize();' and leave the code unpatched. Doesn't cost 
much (especially considering the rest of the code) and works independent 
of the compiler/platform.

> 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.
What happens if either the namespace or class name contains dots? Are 
they converted to underscores? Also, I think it might be interesting to 
be able to override these generated names, perhaps using (custom) code 
attributes, to help in replacing or implementing existing libraries, 
plugins, ... Attributes might also be useful to specify how variables 
should be marshaled.

Also, I think that eventually the 'reverse interop code' could be 
generated from an assembly file by using the Reflection framework. You 
could make the code generator a managed application.


More information about the Mono-devel-list mailing list