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() + ")"); } } } }
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); }
public void LoadVM(Dictionary <string, string> options) { string currentDir = Directory.GetCurrentDirectory(); string jvmDll = Path.Combine("jre", "bin", "server", "jvm.dll"); if ((!File.Exists(jvmDll))) { throw new Exception("Error determining the location of the Java Runtime Environment"); } // Set the directory to the location of the JVM.dll. // This will ensure that the API call JNI_CreateJavaVM will work Directory.SetCurrentDirectory(Path.GetDirectoryName(jvmDll)); var args = new JavaVMInitArgs(); args.version = JNIVersion.JNI_VERSION_10; 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) { opt[i++].optionString = Marshal.StringToHGlobalAnsi(kvp.Key.ToString() + "=" + kvp.Value.ToString()); } 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; } } var result = JavaVM.JNI_CreateJavaVM(out IntPtr javaVirtualMachine, out IntPtr environment, &args); if (result != JNIReturnValue.JNI_OK) { throw new Exception("Cannot create JVM " + result.ToString()); } jvm = new JavaVM(javaVirtualMachine); env = new JNIEnv(environment); Directory.SetCurrentDirectory(currentDir); }
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); }
public void LoadVM(Dictionary <string, string> options, bool AddToExistingJVM) { // Get the location of the current version of the JVM.dll string jreVersion = (string)Registry.GetValue(JRE_REGISTRY_KEY, "CurrentVersion", null); string keyName = Path.Combine(JRE_REGISTRY_KEY, jreVersion); string jvmDir = (string)Registry.GetValue(keyName, "RuntimeLib", null); if ((jvmDir.Length == 0) || (!File.Exists(jvmDir))) { throw new Exception("Error determining the location of the Java Runtime Environment"); } // Set the directory to the location of the JVM.dll. // This will ensure that the API call JNI_CreateJavaVM will work Directory.SetCurrentDirectory(Path.GetDirectoryName(jvmDir)); var args = new JavaVMInitArgs(); switch (Convert.ToInt32((decimal.Parse(jreVersion.Substring(0, 3)) - 1) / 2 * 10)) { case 0: throw new Exception("Unsupported java version. Please upgrade your version of the JRE."); case 1: args.version = JNIVersion.JNI_VERSION_1_2; break; case 2: args.version = JNIVersion.JNI_VERSION_1_4; break; default: args.version = JNIVersion.JNI_VERSION_1_6; break; } 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) { opt[i++].optionString = Marshal.StringToHGlobalAnsi(kvp.Key.ToString() + "=" + kvp.Value.ToString()); } 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); } }