Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" JavaVM *g_JavaVM = nullptr;
- void setJVM(JavaVM *vm)
- {
- g_JavaVM = vm;
- }
- JavaVM *getJVM()
- {
- return g_JavaVM;
- }
- jint JNI_OnLoad(JavaVM* jvm, void* /*reserved*/)
- {
- JNIEnv *env;
- if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
- {
- return -1;
- }
- LOGI("JNI", "jvm(%p) on loading", jvm);
- setJVM(jvm);
- return JNI_VERSION_1_6;
- }
- void JNI_OnUnload(JavaVM* jvm, void* /*reserved*/)
- {
- JNIEnv *env;
- if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) {
- return;
- }
- LOGI("JNI", "jvm(%p) on unloading", getJVM());
- setJVM(NULL);
- }
- #define ATTACH_JVM(jni_env) \
- JNIEnv *g_env = NULL;\
- int env_status = getJVM()->GetEnv((void **)&g_env, JNI_VERSION_1_6); \
- bool bShouldDetach = false; \
- if(env_status == JNI_EDETACHED) {\
- jint attachResult = getJVM()->AttachCurrentThread(&jni_env, NULL); \
- if(attachResult >= 0) \
- bShouldDetach = true;\
- else \
- jni_env = NULL; \
- }\
- else if(JNI_OK == env_status){ \
- jni_env = g_env; \
- }\
- else {\
- jni_env = NULL; \
- }
- #define DETACH_JVM(jni_env) { if( bShouldDetach ){ getJVM()->DetachCurrentThread(); }}
Add Comment
Please, Sign In to add comment