dservice
1#include <cstdlib>2#include "jnienv.h"3
4static JavaVM *sJavaVM = nullptr;5
6JNIEnv *getJniEnv() {7JNIEnv *env = nullptr;8
9switch (sJavaVM->GetEnv((void **) &env, JNI_VERSION_1_6)) {10case JNI_OK:11return env;12
13case JNI_EDETACHED: {14JavaVMAttachArgs args;15args.name = nullptr;16args.group = nullptr;17args.version = JNI_VERSION_1_6;18
19if (!sJavaVM->AttachCurrentThreadAsDaemon(&env, &args)) {20return env;21}22break;23}24}25
26return nullptr;27};28
29extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *aJavaVM, void *aReserved) {30sJavaVM = aJavaVM;31return JNI_VERSION_1_6;32}
33
34extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *aJavaVM, void *aReserved) {35sJavaVM = nullptr;36}