Advertisement
Guest User

Untitled

a guest
May 4th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. static jboolean init (JNIEnv* env, jobject obj) {...}
  2.  
  3. bool GetJniEnv(JavaVM *vm, JNIEnv **env) {
  4. bool did_attach_thread = false;
  5. *env = nullptr;
  6. // Check if the current thread is attached to the VM
  7. auto get_env_result = vm->GetEnv((void**)env, JNI_VERSION_1_6);
  8. if (get_env_result == JNI_EDETACHED) {
  9. if (vm->AttachCurrentThread(env, NULL) != JNI_OK) {
  10. // Failed to attach thread. Throw an exception if you want to.
  11. }
  12. did_attach_thread = true;
  13. } else if (get_env_result == JNI_EVERSION) {
  14. // Unsupported JNI version. Throw an exception if you want to.
  15. }
  16. return did_attach_thread;
  17. }
  18.  
  19. JNIEnv *env;
  20. bool did_attach = GetJniEnv(vm, &env);
  21. // Use env...
  22. // ...
  23. if (did_attach) {
  24. vm->DetachCurrentThread();
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement