The following java snippet calls a jni function Java_org_suhail_keylogger_HelperClasses_NativeMethods_unregisterHook
:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1.setEnabled(false);
jMenuItem2.setEnabled(true);
try {
System.loadLibrary("Dll_PKeylogger"); // Load the dll written to listen to the tapping of keys
nativeMethods.initializeJNIVars(); // called upon the object of a class named NativeMethods
}catch(Exception exc) {
exc.printStackTrace();
}
}
NativeMethods (class,whose object is used to call a JNI C method above) :
public class NativeMethods {
public native void initializeJNIVars();
public native void unregisterHook();
public void displayKeyStrokes() { // FUNCTION THAT IS CALLED BACK FROM JNI C CODE
System.out.println("Java Message : A Key has been pressed");
}
}
JNI C method, called by the java code :
void Java_org_suhail_keylogger_HelperClasses_NativeMethods_initializeJNIVars
(JNIEnv *env, jobject obj) {
jclass cls = (*env)->GetObjectClass(env,obj);
callBackToDeliverKeyStroke = (*env)->GetMethodID(env,cls,"displayKeyStrokes","()V");
object = (*env)->NewGlobalRef(env,obj);
if(object == NULL | callBackToDeliverKeyStroke == NULL | cls == NULL) {
printf("Initialization error...One of the variable is Null\n");
}
}
Method in the same module as the above method that calls the java function:
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
JNIEnv *Env;
(*Env)->CallVoidMethod(Env,object,callBackToDeliverKeyStroke);
// I have initialized object and callBackToDeliverKeyStroke in the above method
}
As the execution reaches the last point of execution i.e the function just mentioned above JVM
crashes. Why is that ? Where have I made a mistake ?