예제 #1
0
 private void LoadNecessaryDll()
 {
     hNecessaryDll    = new int[JavaBasicConst.NECESSARY_DLL_NUMBER];
     hNecessaryDll[0] = NativeAPI.LoadLibrary(JavaBasicConst.N1_DLL);
 }
예제 #2
0
        public bool InitializeJVM(string[] jvmoptions)
        {
            //LoadNecessaryDll();

            hJvmDll = NativeAPI.LoadLibrary(jvm_path);
            if (0 == hJvmDll)
            {
                return(false);
            }
            m_JNI_CreateJavaVM pCreateJavaVM =
                (m_JNI_CreateJavaVM)NativeAPI.GetProcDelegateEx(hJvmDll, "JNI_CreateJavaVM", typeof(m_JNI_CreateJavaVM));

            if (null == pCreateJavaVM)
            {
                return(false);
            }

            IntPtr[] optsptr = new IntPtr[jvmoptions.Length];
            for (int i = 0; i < jvmoptions.Length; i++)
            {
                optsptr[i] = IntPtr.Zero;
            }
            int[]  optstmp = new int[2 * jvmoptions.Length];
            IntPtr options = IntPtr.Zero;

            int[] jvm_p = { 0 };
            int[] env_p = { 0 };
            int   jvmp = 0, envp = 0;

            int[]  jvmflist = new int[JavaBasicConst.JNI_JVM_INT];
            int[]  envflist = new int[JavaBasicConst.JNI_ENV_INT];
            IntPtr jvm_p_p = IntPtr.Zero, env_p_p = IntPtr.Zero;

            // init options
            try
            {
                int i;
                for (i = 0; i < jvmoptions.Length; i++)
                {
                    optsptr[i]         = NativeMemory.NewString(jvmoptions[i]);
                    optstmp[i * 2]     = optsptr[i].ToInt32();
                    optstmp[i * 2 + 1] = 0;
                }
                options = NativeMemory.NewSpace(sizeof(int) * optstmp.Length);
                NativeMemory.CopyInt(options, optstmp);

                JavaVMInitArgs args = new JavaVMInitArgs();
                args.version = JavaBasicConst.JNI_VERSION_1_6;
                //args.version = JavaBasicConst.JNI_VERSION_1_7;
                args.nOptions           = 3;
                args.options            = options.ToInt32();
                args.ignoreUnrecognized = JavaBasicConst.JNI_TRUE;

                // init jvm,env
                for (i = 0; i < jvmflist.Length; i++)
                {
                    jvmflist[i] = 0;
                }
                for (i = 0; i < envflist.Length; i++)
                {
                    envflist[i] = 0;
                }

                jvm_p_p = NativeMemory.NewSpace(sizeof(int));
                env_p_p = NativeMemory.NewSpace(sizeof(int));

                // create a jvm
                int _res = pCreateJavaVM(jvm_p_p, env_p_p, ref args);
                if (_res < 0)
                {
                    throw new Exception("failed to create a jvm");
                }

                NativeMemory.CopyIntBack(jvm_p_p, jvm_p);
                NativeMemory.CopyIntBack(env_p_p, env_p);
                jvmp = jvm_p[0];
                envp = env_p[0];
                NativeMemory.CopyIntBack(new IntPtr(jvmp), jvm_p);
                NativeMemory.CopyIntBack(new IntPtr(envp), env_p);
                NativeMemory.CopyIntBack(new IntPtr(jvm_p[0]), jvmflist);
                NativeMemory.CopyIntBack(new IntPtr(env_p[0]), envflist);

                // TODO: process  jvmflist, envflist
                jvm = new JavaVM(jvmp, jvmflist);
                env = new JavaENV(envp, envflist);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            // dispose IntPtr
            NativeMemory.Dispose(options);
            foreach (IntPtr x in optsptr)
            {
                NativeMemory.Dispose(x);
            }
            NativeMemory.Dispose(jvm_p_p);
            NativeMemory.Dispose(env_p_p);

            return(true);
        }