Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static jboolean init (JNIEnv* env, jobject obj) {...}
- bool GetJniEnv(JavaVM *vm, JNIEnv **env) {
- bool did_attach_thread = false;
- *env = nullptr;
- // Check if the current thread is attached to the VM
- auto get_env_result = vm->GetEnv((void**)env, JNI_VERSION_1_6);
- if (get_env_result == JNI_EDETACHED) {
- if (vm->AttachCurrentThread(env, NULL) != JNI_OK) {
- // Failed to attach thread. Throw an exception if you want to.
- }
- did_attach_thread = true;
- } else if (get_env_result == JNI_EVERSION) {
- // Unsupported JNI version. Throw an exception if you want to.
- }
- return did_attach_thread;
- }
- JNIEnv *env;
- bool did_attach = GetJniEnv(vm, &env);
- // Use env...
- // ...
- if (did_attach) {
- vm->DetachCurrentThread();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement