Wednesday, April 24, 2013

jni jnienv GetJavaVM example c c++ java


GetJavaVM


jint GetJavaVM(JNIEnv *env, JavaVM **vm);

Returns the Java VM interface (used in the Invocation API) associated with the current thread. The result is placed at the location pointed to by the second argument, vm. (GetJavaVM)
LINKAGE:
Index 219 in the JNIEnv interface function table.
PARAMETERS of GetJavaVM

env: the JNI interface pointer.

vm: a pointer to where the result should be placed.

RETURNS of GetJavaVM

Returns “0” on success; returns a negative value on failure.
Example of GetJavaVM
static JavaVM *jvm;
JNIEXPORT void JNICALL Java_SomeClass_init(JNIEnv *env, jclass) {
    int status = (*env)->GetJavaVM(env, &jvm);
    if(status != 0) {
        // Fail!
    }
}
Now you can use that JavaVM* to get the current JNIEnv* with AttachCurrentThread:
dsmResult_t dsmInitializeCall( dsmResult_t status, void * pUserData, dsmEvent_t * hEvent ) {
    JNIEnv *env;
    (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
    (*env)->CallVoidMethod(env, classObject, mid);
}