[Mono-dev] Registering internal calls at runtime

Michel Boissé mboisse at noesisinnovation.net
Tue Jul 13 15:17:32 EDT 2010


Here's the simplest use case which demonstrates our problem. It consists of 
two projects (an executable assembly and a dll implemented in C). Here's the 
simplest code to reproduce it:

1) Console Application Assembly with Program.cs containing the following 
code:
        using System;
        using System.Runtime.CompilerServices;
        using System.Runtime.InteropServices;

        namespace MonoInternals
        {
            public static class Registrar
            {
                [DllImport("CInternals.dll")]
                public extern static void RegisterMethods();
            }

            public class InternallyImplementedClass
            {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string InternalMethod();
            }

            class Program
            {
                static void Main(string[] args)
                {
                    Registrar.RegisterMethods();
                    Console.Write(InternallyImplementedClass.InternalMethod());
                }
            }
        }

2) A dynamic library containing CInternals.c:
        #include <mono/mini/jit.h>
        #include <mono/metadata/environment.h>
        #include <stdlib.h>

        MonoString*
        InternalMethod()
        {
            return mono_string_new (mono_domain_get (), "InternalMethod 
called successfully\n\n");
        }

        __declspec(dllexport) void
        RegisterMethods()
        {
            mono_add_internal_call 
("MonoInternals.InternallyImplementedClass::InternalMethod", 
InternalMethod);
        }


Registering methods immediately before mono_jit_exec( ) fixes it.


--------------------------------------------------
From: "Robert Jordan" <robertj at gmx.net>
Sent: Tuesday, July 13, 2010 7:56 AM
To: <mono-devel-list at lists.ximian.com>
Subject: Re: [Mono-dev] Registering internal calls at runtime

> Hi Michael,
>
> On 13.07.2010 01:05, Michel Boissé wrote:
>> Thank you Robert, I appreciate your involvement.
>>
>> Here's our problem: any calls to mono_add_internal_call is ineffective 
>> when
>> made from managed code.
>> Our initial idea was to use P/Invoke to load a dynamic library and run 
>> some
>> initialization method in it which would make the appropriate 
>> registrations.
>> Based on tests we did, any call which doesn't precede the mono_jit_exec 
>> call
>> has no effect.
>
> I've actually tested this before I've answered your post: adding
> internal calls after the first mono_jit_exec() is supported.
>
> You can even override existing icalls as long as the affected
> methods were not already JITed.
>
> Robert
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
> 



More information about the Mono-devel-list mailing list