Esempio n. 1
0
 internal JNIEnv(IntPtr native)
 {
     this.envPtr = native;
     functions   = *(*(JavaPtr *)native.ToPointer()).functions;
     InitMethods();
     if (defaultVM == null)
     {
         defaultVM = GetJavaVM();
     }
 }
Esempio n. 2
0
 internal JNIEnv(IntPtr native)
 {
     this.envPtr = native;
     functions = *(*(JavaPtr*) native.ToPointer()).functions;
     InitMethods();
     if (defaultVM == null)
     {
         defaultVM = GetJavaVM();
     }
 }
Esempio n. 3
0
File: JNI.cs Progetto: gywerd/WinJNI
        public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, bool attachIfExists, params string[] options)
        {
            Init();
            IntPtr njvm;
            IntPtr nenv;
            var    args = new JavaVMInitArgs();

            args.version = JNI_VERSION_1_4;

            if (options.Length > 0)
            {
                args.nOptions = options.Length;
                var opt = new JavaVMOption[options.Length];
                for (int i = 0; i < options.Length; i++)
                {
                    opt[i].optionString = Marshal.StringToHGlobalAnsi(options[i]);
                }

                fixed(JavaVMOption *a = &opt[0])
                {
                    args.options = a;
                }
            }
            JNIResult result;

            if (attachIfExists)
            {
                IntPtr njvma;
                int    count;
                result = Dll.JNI_GetCreatedJavaVMs(out njvma, 1, out count);
                if (result != JNIResult.JNI_OK)
                {
                    throw new JNIException("Can't enumerate current JVMs " + result);
                }
                if (count > 0)
                {
                    njvm   = njvma;
                    jvm    = new JavaVM(njvm);
                    result = jvm.AttachCurrentThread(out env, args);
                    if (result != JNIResult.JNI_OK)
                    {
                        throw new JNIException("Can't join current JVM " + result);
                    }
                    return;
                }
            }
            result = Dll.JNI_CreateJavaVM(out njvm, out nenv, &args);
            if (result != JNIResult.JNI_OK)
            {
                Console.Error.WriteLine("Can't load JVM (already have one ?)");
                throw new JNIException("Can't load JVM (already have one ?) " + result);
            }
            jvm = new JavaVM(njvm);
            env = new JNIEnv(nenv);
        }
Esempio n. 4
0
 public static JNIEnv GetEnvNoThrow(JavaVM vm)
 {
     if (threadJNIEnv == null)
     {
         JNIResult result = vm.AttachCurrentThreadAsDaemon(out threadJNIEnv, null);
         if (result != JNIResult.JNI_OK)
         {
             return(null);
         }
     }
     return(threadJNIEnv);
 }
Esempio n. 5
0
 public static JNIEnv GetEnvForVm(JavaVM vm)
 {
     if (threadJNIEnv == null)
     {
         JNIResult result = vm.AttachCurrentThreadAsDaemon(out threadJNIEnv, null);
         if (result != JNIResult.JNI_OK)
         {
             throw new JNIException("AttachCurrentThreadAsDaemon failed: " + result);
         }
     }
     return(threadJNIEnv);
 }
Esempio n. 6
0
 public static JNIEnv GetEnvForVm(JavaVM vm)
 {
     if (threadJNIEnv == null)
     {
         JNIResult result = vm.AttachCurrentThreadAsDaemon(out threadJNIEnv, null);
         if (result != JNIResult.JNI_OK)
         {
             throw new JNIException("AttachCurrentThreadAsDaemon failed: " + result);
         }
     }
     return threadJNIEnv;
 }
Esempio n. 7
0
        public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, bool attachIfExists, params string[] options)
        {
            Init();
            IntPtr njvm;
            IntPtr nenv;
            var args = new JavaVMInitArgs();
            args.version = JNI_VERSION_1_4;

            if (options.Length > 0)
            {
                args.nOptions = options.Length;
                var opt = new JavaVMOption[options.Length];
                for (int i = 0; i < options.Length; i++)
                {
                    opt[i].optionString = Marshal.StringToHGlobalAnsi(options[i]);
                }
                fixed (JavaVMOption* a = &opt[0])
                {
                    args.options = a;
                }
            }
            JNIResult result;
            if (attachIfExists)
            {
                IntPtr njvma;
                int count;
                result = Dll.JNI_GetCreatedJavaVMs(out njvma, 1, out count);
                if (result != JNIResult.JNI_OK)
                {
                    throw new JNIException("Can't enumerate current JVMs " + result);
                }
                if (count > 0)
                {
                    njvm = njvma;
                    jvm = new JavaVM(njvm);
                    result = jvm.AttachCurrentThread(out env, args);
                    if (result != JNIResult.JNI_OK)
                    {
                        throw new JNIException("Can't join current JVM " + result);
                    }
                    return;
                }
            }
            result = Dll.JNI_CreateJavaVM(out njvm, out nenv, &args);
            if (result != JNIResult.JNI_OK)
            {
                Console.Error.WriteLine("Can't load JVM (already have one ?)");
                throw new JNIException("Can't load JVM (already have one ?) " + result);
            }
            jvm = new JavaVM(njvm);
            env = new JNIEnv(nenv);
        }
Esempio n. 8
0
 public JavaVM GetJavaVM()
 {
     if (javaVM == null)
     {
         // no need to lock, it will be always on current thread
         //lock (this)
         {
             IntPtr jvm;
             getJavaVM.Invoke(envPtr, out jvm);
             ExceptionTest();
             javaVM = new JavaVM(jvm);
         }
     }
     return(javaVM);
 }
Esempio n. 9
0
 public static JNIEnv GetEnvNoThrow(JavaVM vm)
 {
     if (threadJNIEnv == null)
     {
         JNIResult result = vm.AttachCurrentThreadAsDaemon(out threadJNIEnv, null);
         if (result != JNIResult.JNI_OK)
         {
             return null;
         }
     }
     return threadJNIEnv;
 }
Esempio n. 10
0
 public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, params string[] options)
 {
     CreateJavaVM(out jvm, out env, false, options);
 }
Esempio n. 11
0
		public static void SetJVM(JavaVM jvm)
		{
			jvmLoaded = true;
			clrLoaded = true;
			setup = new BridgeSetup();
		}
Esempio n. 12
0
 public JniGlobalHandle(JniGlobalHandleNs handleValue, JavaVM javaVM)
     : base(IntPtr.Zero, true)
 {
     this.javaVM = javaVM;
     SetHandle(handleValue.handle);
 }
Esempio n. 13
0
File: JNI.cs Progetto: gywerd/WinJNI
 public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, params string[] options)
 {
     CreateJavaVM(out jvm, out env, false, options);
 }