// JNI entry point, This is executed when the Java virtual machine attaches to the native library. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *UNUSED(reserved)) { /* Grab the currently running virtual machine so we can attach to it in * functions that are not called from java. ( I.E. ThreadProc ) */ jvm = vm; JNIEnv *env = NULL; if ((*jvm)->GetEnv(jvm, (void **)(&env), jni_version) == JNI_OK) { #ifdef DEBUG fprintf(stdout, "JNI_OnLoad(): GetEnv() successful.\n"); #endif // Create all the global class references onload to prevent class loader // issues with JNLP and some IDE's. if (CreateJNIGlobals() == RETURN_FAILURE) { #ifdef DEBUG fprintf(stderr, "JNI_OnLoad(): CreateJNIGlobals() failed!\n"); #endif ThrowFatalError("Failed to locate one or more required classes."); } // Run platform specific load items. OnLibraryLoad(); // Set java properties from native sources. SetNativeProperties(env); } else { #ifdef DEBUG fprintf(stderr, "JNI_OnLoad(): GetEnv() failed!\n"); #endif ThrowFatalError("Failed to aquire JNI interface pointer"); } #ifdef DEBUG fprintf(stdout, "JNI_Load(): JNI Loaded.\n"); #endif return jni_version; }
int CreateJNIGlobals() { int status = RETURN_FAILURE; JNIEnv *env = NULL; if ((*jvm)->GetEnv(jvm, (void **)(&env), jni_version) == JNI_OK) { // Class and getInstance method id for the GlobalScreen Object. jclass clsLocalThread = (*env)->FindClass(env, "java/lang/Thread"); if (clsLocalThread != NULL) { clsThread = (jclass) (*env)->NewGlobalRef(env, clsLocalThread); // Get the method ID for GlobalScreen.getInstance() idCurrentThread = (*env)->GetStaticMethodID(env, clsThread, "currentThread", "()Ljava/lang/Thread;"); #ifdef DEBUG if (idCurrentThread == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for Thread.currentThread()!\n"); } #endif // Get the method ID for GlobalScreen.dispatchEvent(). idSetName = (*env)->GetMethodID(env, clsThread, "setName", "(Ljava/lang/String;)V"); #ifdef DEBUG if (idSetName == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for Thread.setName(String)!\n"); } #endif } #ifdef DEBUG else { fprintf(stderr, "CreateJNIGlobals(): Failed to locate the Thread class!\n"); } #endif // Class and getInstance method id for the GlobalScreen Object. jclass clsLocalGlobalScreen = (*env)->FindClass(env, "org/jnativehook/GlobalScreen"); if (clsLocalGlobalScreen != NULL) { clsGlobalScreen = (jclass) (*env)->NewGlobalRef(env, clsLocalGlobalScreen); // Get the method ID for GlobalScreen.getInstance() idGetInstance = (*env)->GetStaticMethodID(env, clsGlobalScreen, "getInstance", "()Lorg/jnativehook/GlobalScreen;"); #ifdef DEBUG if (idGetInstance == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for GlobalScreen.getInstance()!\n"); } #endif // Get the method ID for GlobalScreen.dispatchEvent(). idDispatchEvent = (*env)->GetMethodID(env, clsGlobalScreen, "dispatchEvent", "(Lorg/jnativehook/NativeInputEvent;)V"); #ifdef DEBUG if (idDispatchEvent == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for GlobalScreen.dispatchEvent()!\n"); } #endif // Get the method ID for GlobalScreen.startEventDispatcher(). idStartEventDispatcher = (*env)->GetMethodID(env, clsGlobalScreen, "startEventDispatcher", "()V"); #ifdef DEBUG if (idStartEventDispatcher == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for GlobalScreen.startEventDispatcher()!\n"); } #endif // Get the method ID for GlobalScreen.stopEventDispatcher(). idStopEventDispatcher = (*env)->GetMethodID(env, clsGlobalScreen, "stopEventDispatcher", "()V"); #ifdef DEBUG if (idStopEventDispatcher == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for GlobalScreen.stopEventDispatcher()!\n"); } #endif } #ifdef DEBUG else { fprintf(stderr, "CreateJNIGlobals(): Failed to locate the GlobalScreen class!\n"); } #endif // Class and Constructor for the NativeKeyEvent Object. jclass clsLocalKeyEvent = (*env)->FindClass(env, "org/jnativehook/keyboard/NativeKeyEvent"); if (clsLocalKeyEvent != NULL) { clsKeyEvent = (jclass) (*env)->NewGlobalRef(env, clsLocalKeyEvent); idKeyEvent = (*env)->GetMethodID(env, clsKeyEvent, "<init>", "(IJIIICI)V"); #ifdef DEBUG if (idKeyEvent == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for NativeKeyEvent.NativeKeyEvent(int, long, int, int, int, int)!\n"); } #endif } #ifdef DEBUG else { fprintf(stderr, "CreateJNIGlobals(): Failed to locate the NativeKeyEvent class!\n"); } #endif // Class and Constructor for the NativeMouseEvent Object. jclass clsLocalMouseEvent = (*env)->FindClass(env, "org/jnativehook/mouse/NativeMouseEvent"); if (clsLocalMouseEvent != NULL) { clsMouseEvent = (jclass) (*env)->NewGlobalRef(env, clsLocalMouseEvent); idMouseButtonEvent = (*env)->GetMethodID(env, clsMouseEvent, "<init>", "(IJIIIII)V"); #ifdef DEBUG if (idMouseButtonEvent == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for NativeMouseEvent.NativeMouseEvent(int, long, int, int, int, int)!\n"); } #endif idMouseMotionEvent = (*env)->GetMethodID(env, clsMouseEvent, "<init>", "(IJIIII)V"); #ifdef DEBUG if (idMouseMotionEvent == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for NativeMouseEvent.NativeMouseEvent(int, long, int, int, int)!\n"); } #endif } #ifdef DEBUG else { fprintf(stderr, "CreateJNIGlobals(): Failed to locate the NativeMouseEvent class!\n"); } #endif // Class and Constructor for the NativeMouseWheelEvent Object. jclass clsLocalMouseWheelEvent = (*env)->FindClass(env, "org/jnativehook/mouse/NativeMouseWheelEvent"); if (clsLocalMouseWheelEvent != NULL) { clsMouseWheelEvent = (jclass) (*env)->NewGlobalRef(env, clsLocalMouseWheelEvent); idMouseWheelEvent = (*env)->GetMethodID(env, clsMouseWheelEvent, "<init>", "(IJIIIIIII)V"); #ifdef DEBUG if (idMouseWheelEvent == NULL) { fprintf(stderr, "CreateJNIGlobals(): Failed to acquire the method ID for NativeMouseWheelEvent.NativeMouseWheelEvent(int, long, int, int, int, int, int, int)!\n"); } #endif } #ifdef DEBUG else { fprintf(stderr, "CreateJNIGlobals(): Failed to locate the NativeMouseWheelEvent class!\n"); } #endif // Check and make sure everything is correct. if ((*env)->ExceptionCheck(env) == JNI_FALSE) { status = RETURN_SUCCESS; } } else { // We cant do a whole lot of anything if we cant attach to the current thread. #ifdef DEBUG fprintf(stderr, "CreateJNIGlobals(): GetEnv() failed!\n"); #endif ThrowFatalError("Failed to aquire JNI interface pointer"); } return status; }