Exemplo n.º 1
0
 private static bool CheckAlreadyLoaded(BridgeSetup newSetup, JNIEnv env)
 {
     if (clrLoaded)
     {
         return(true);
     }
     if (newSetup.BindNative)
     {
         JniLocalHandle br = env.FindClassPtrNoThrow("net/sf/jni4net/Bridge");
         if (JniLocalHandle.IsNull(br))
         {
             throw new JNIException("Can't find class net.sf.jni4net.Bridge on classpath");
         }
         IntPtr ir           = env.GetStaticFieldIDPtr(br, "isRegistered", "Z");
         bool   isRegistered = env.GetStaticBooleanField(br, ir);
         if (isRegistered)
         {
             // rather neat. When called like Clr->Java->Clr this dll gets loaded twice.
             // which means two separate sets CLR of classes and static members
             if (newSetup.Verbose)
             {
                 Console.WriteLine("Already initialized jni4net core before");
             }
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 2
0
        internal static int initDotNetImpl(IntPtr envi, IntPtr clazz)
        {
            JNIEnv env = null;

            try
            {
                try
                {
                    env = JNIEnv.Wrap(envi);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Can't bind env:\n" + ex.Message);
                    Console.Error.WriteLine("Can't bind env:\n" + ex);
                    return(-3);
                }
                JniLocalHandle br = env.FindClassPtrNoThrow("net/sf/jni4net/Bridge");
                if (JniLocalHandle.IsNull(br))
                {
                    Console.Error.WriteLine("Can't find net/sf/jni4net/Bridge class");
                    return(-2);
                }
                IntPtr vb = env.GetStaticFieldIDPtr(br, "verbose", "Z");
                IntPtr db = env.GetStaticFieldIDPtr(br, "debug", "Z");
                if (IntPtr.Zero == vb || IntPtr.Zero == db)
                {
                    Console.Error.WriteLine("Can't find verbose or debug fields");
                    return(-6);
                }
                var newSetup = new BridgeSetup(false)
                {
                    Verbose    = env.GetStaticBooleanField(br, vb),
                    Debug      = env.GetStaticBooleanField(br, db),
                    BindStatic = true,
                    BindNative = true
                };
                BindCore(env, newSetup);
                if (Setup.JavaHome == null)
                {
                    Setup.JavaHome = java.lang.System.getProperty("java.home");
                }
                DumpRuntimeVersions();
            }
            catch (Exception ex)
            {
                try
                {
                    Console.Error.WriteLine("Can't bind bridge:" + ex.Message);
                    Console.Error.WriteLine("Can't bind bridge:" + ex);
                    Class exClazz = env.FindClass("net/sf/jni4net/inj/INJException");
                    env.ThrowNew(exClazz, ex.Message);
                    return(-4);
                }
                catch (Exception)
                {
                    return(-5);
                }
            }
            return(0);
        }
Exemplo n.º 3
0
 private static void CurrentDomain_DomainUnload(object sender, EventArgs e)
 {
     if (Setup.BindNative)
     {
         JNIEnv env = JNIEnv.ThreadEnv;
         Registry.UnregisterNative();
         JniLocalHandle br = env.FindClassPtrNoThrow("net/sf/jni4net/Bridge");
         IntPtr         ir = env.GetStaticFieldIDPtr(br, "isRegistered", "Z");
         env.SetStaticBooleanField(br, ir, false);
     }
 }