public static JavaEnvWrapper init( JNI_VERSION version = JNI_VERSION.v1_6, string[] options = null, bool ignoreUnrecognized = false ) { IntPtr optionsPtr = IntPtr.Zero; if (options != null && options.Length > 0) { int optSize = Marshal.SizeOf <JavaVMOption>(); //Leave the trash laying around cause this is a singleton //Also cause I don't know when java will try to read these optionsPtr = Marshal.AllocHGlobal(optSize * options.Length); var optArray = (JavaVMOption *)optionsPtr.ToPointer(); optArray[1] = new JavaVMOption(); for (int i = 0; i < options.Length; i++) { optArray[i].optionString = Marshal.StringToHGlobalAnsi(options[i]); optArray[i].extraInfo = IntPtr.Zero; } } var result = new JavaEnvWrapper( version: version, nOptions: options?.Length ?? 0, options: optionsPtr, ignoreUnrecognized: (byte)(ignoreUnrecognized ? 1 : 0) ); return(result); }
private JavaEnvWrapper( JNI_VERSION version = JNI_VERSION.v1_6, int nOptions = 0, IntPtr options = default, byte ignoreUnrecognized = 0, string threadName = null ) { lock (_JVMLinkLock) loadJavaLibrary(); this.version = version; if (JVMInstance == null) { var args = new JavaVMInitArgs { version = version, nOptions = nOptions, options = options, ignoreUnrecognized = ignoreUnrecognized }; int retc = _JNI_CreateJavaVM( out _jvm, out _env, ref args ); JVMInstance = this._jvm; } else { this._jvm = JVMInstance; var namePtr = Marshal.StringToHGlobalAnsi(threadName); var args = new JavaVMAttachArgs { version = version, name = namePtr, extraInfo = IntPtr.Zero }; Marshal.GetDelegateForFunctionPointer <_AttachCurrentThread>( JVMInstance->fTable->AttachCurrentThread ).Invoke( JVMInstance, out _env, ref args ); } this._findClass = Marshal.GetDelegateForFunctionPointer <_FindClassD>(_env->fTable->FindClass); this._GetStaticMethodID = Marshal.GetDelegateForFunctionPointer <_GetStaticMethodIDD>(_env->fTable->GetStaticMethodID); this._CallStaticMethod = Marshal.GetDelegateForFunctionPointer <_CallStaticMethodD>(_env->fTable->CallStaticObjectMethodA); }