On 12.07.2012 19:15, Manski wrote:
When I load a native shared library with "JavaSystem.Load()" in a static
constructor like this:

   class MyClass {
     static MyClass() {
       Java.Lang.JavaSystem.Load("/path/to/libmylib.so");
     }

     [DllImport("mylib")]
     private static extern int get_version();

     public static int getVersion() {
       return get_version();
     }
   }

Why do the following code fail (with DllNotFoundException)?

   int x = MyClass.getVersion();

But this code (instead) succeeds:

   var unused = new MyClass();
   int x = MyClass.getVersion();

It's failing because

        Java.Lang.JavaSystem.Load("/path/to/libmylib.so");

is *executed* after the method

     public static int getVersion() {
       return get_version();
     }

is *compiled*. This is how Mono's JIT is working.

You may get around this by adding another level of indirection:

     public static int getVersion() {
       return getVersionInternal();
     }

     static int getVersionInternal() {
       return get_version();
     }

This way, the methods containing the p/invoke calls will
be compiled after the .cctor has been executed.

Robert

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to