Exemplo n.º 1
0
 private static IntPtr GetEnvironmentHandle(out int unmanagedThreadId, out bool isNew)
 {
     unmanagedThreadId = CurrentUnmanagedThreadId;
     isNew = false;
     IntPtr env = JavaVM.GetEnvironment();
     if (env == IntPtr.Zero) // environment not attached
     {
         string threadName = Thread.CurrentThread.Name;
         if (!(threadName != null && threadName.Length > 0))
         {
             threadName = "{noname_thread}";
         }
         threadName += ", unmanaged thread ID: ";
         threadName += unmanagedThreadId.ToString(CultureInfo.InvariantCulture);
         JavaVMThreadAttachArguments args = new JavaVMThreadAttachArguments(threadName, null);
         env = JavaVM.AttachCurrentThread(args);
         isNew = true;
     }
     return env;
 }
Exemplo n.º 2
0
 internal static IntPtr AttachCurrentThread(JavaVMThreadAttachArguments arguments)
 {
     JavaVMAttachArgs attachArgs = new JavaVMAttachArgs();
     if (arguments != null)
     {
         attachArgs = arguments.ToArgs(Instance.JvmConfiguration.Version);
     }
     try
     {
         unsafe
         {
             void** ppEnv = null;
             void* pArgs = null;
             if (arguments != null)
             {
                 pArgs = &attachArgs;
             }
             int result = Instance.mInvokeInterface.AttachCurrentThread(Instance.mJavaVM, &ppEnv, pArgs);
             if (0 != result)
             {
                 JniException.CheckVMErrorCode(result);
             }
             IntPtr environmentHandle = new IntPtr((void*)ppEnv);
             return environmentHandle;
         }
     }
     finally
     {
         if (arguments != null)
         {
             JavaVMThreadAttachArguments.FreeArgs(attachArgs);
         }
     }
 }