Exemplo n.º 1
0
        private void AttachToCurrentJVM(JavaVMInitArgs args)
        {
            // This is only required if you want to reuse the same instance of the JVM
            // This is especially useful if you are using JNI in a webservice. see page 89 of the
            // Java Native Interface: Programmer's Guide and Specification by Sheng Liang
            if (AttachToCurrentJVMThread)
            {
                int nVMs;

                IntPtr javaVirtualMachine;
                int    res = JavaVM.JNI_GetCreatedJavaVMs(out javaVirtualMachine, 1, out nVMs);
                if (res != JNIReturnValue.JNI_OK)
                {
                    throw new Exception("JNI_GetCreatedJavaVMs failed (" + res.ToString() + ")");
                }
                if (nVMs > 0)
                {
                    jvm = new JavaVM(javaVirtualMachine);
                    res = jvm.AttachCurrentThread(out env, args);
                    if (res != JNIReturnValue.JNI_OK)
                    {
                        throw new Exception("AttachCurrentThread failed (" + res.ToString() + ")");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public int AttachCurrentThreadAsDaemon(out JNIEnv penv, JavaVMInitArgs?args)
        {
            if (_attachCurrentThreadAsDaemon == null)
            {
                GetDelegateForFunctionPointer(_functions.AttachCurrentThreadAsDaemon,
                                              ref _attachCurrentThreadAsDaemon);
            }
            IntPtr env;
            int    result;

            if (!args.Equals(null))
            {
                JavaVMInitArgs value = args.Value;
                result = _attachCurrentThreadAsDaemon.Invoke(_jvm, out env, &value);
            }
            else
            {
                result = _attachCurrentThreadAsDaemon.Invoke(_jvm, out env, null);
            }
            if (result == JNIReturnValue.JNI_OK)
            {
                penv = new JNIEnv(env);
            }
            else
            {
                penv = null;
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <param name="AddToExistingJVM"></param>
        /// <param name="targetVersion">Specified the version</param>
        public void LoadVM(Dictionary <string, string> options, bool AddToExistingJVM = false, JNIVersion targetVersion = JNIVersion.JNI_VERSION_10)
        {
            // Set the directory to the location of the JVM.dll.
            // This will ensure that the API call JNI_CreateJavaVM will work
            // TODO : maybe remove this after changing DLLImport by Assembly dynamic loading
            // This probably break pathes to ressources
            // Directory.SetCurrentDirectory(Path.GetDirectoryName(JavaNativeInterface.__jvmDllPath));

            var args = new JavaVMInitArgs();

            args.version = (int)targetVersion;

            args.ignoreUnrecognized = JavaVM.BooleanToByte(true); // True

            if (options.Count > 0)
            {
                args.nOptions = options.Count;
                var opt = new JavaVMOption[options.Count];
                int i   = 0;
                foreach (KeyValuePair <string, string> kvp in options)
                {
                    string optionString = kvp.Key.ToString() + "=" + kvp.Value.ToString();
                    opt[i++].optionString = Marshal.StringToHGlobalAnsi(optionString);
                }

                fixed(JavaVMOption *a = &opt[0])
                {
                    // prevents the garbage collector from relocating the opt variable as this is used in unmanaged code that the gc does not know about
                    args.options = a;
                }
            }

            if (!AttachToCurrentJVMThread)
            {
                IntPtr environment;
                IntPtr javaVirtualMachine;
                int    result = JavaVM.JNI_CreateJavaVM(out javaVirtualMachine, out environment, &args);
                if (result != JNIReturnValue.JNI_OK)
                {
                    throw new Exception("Cannot create JVM " + result.ToString());
                }

                jvm = new JavaVM(javaVirtualMachine);
                env = new JNIEnv(environment);
            }
            else
            {
                AttachToCurrentJVM(args);
            }
        }
Exemplo n.º 4
0
        internal int AttachCurrentThread(out JNIEnv penv, JavaVMInitArgs?args)
        {
            if (_attachCurrentThread == null)
            {
                //attachCurrentThread = (JNIInvokeInterface_.AttachCurrentThread)Marshal.GetDelegateForFunctionPointer(functions.AttachCurrentThread, typeof(JNIInvokeInterface_.AttachCurrentThread));
                GetDelegateForFunctionPointer(_functions.AttachCurrentThread, ref _attachCurrentThread);
            }
            IntPtr env;
            int    result;

            if (args.HasValue)
            {
                JavaVMInitArgs initArgs = args.Value;
                result = _attachCurrentThread.Invoke(_jvm, out env, &initArgs);
            }
            else
            {
                result = _attachCurrentThread.Invoke(_jvm, out env, null);
            }
            penv = new JNIEnv(env);
            return(result);
        }