In my JNI C++ Code, how can i check if the current thread has already been attached to a VM?

Peter Mularien

Here is an excerpt from the Java 2 JNI documentation:

jint GetEnv(JavaVM *vm, void **env, jint version);
     If the current thread is not attached to the VM, sets *env to NULL, and returns JNI_EDETACHED. If the specified version is not supported, sets *env to NULL, and returns JNI_EVERSION.
     Otherwise, sets *env to the appropriate interface, and returns JNI_OK. 
So you'd use it like this:

if (theVM->GetEnv (theVm, (void *) &env, JNI_VERSION_1_2) == JNI_EDETACHED) {
   // detached
} else {
  // attached