0
以下Java代碼段調用一個JNI函數Java_org_suhail_keylogger_HelperClasses_NativeMethods_unregisterHook
:爲什麼jvm崩潰,我做了一個java函數的回調?
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(類,其目的是用於調用上述一個JNI C法):
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法,由java代碼調用:
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");
}
}
方法相同的模塊調用Java函數上述方法中:
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
}
如執行到達執行的即上述JVM
崩潰剛纔提到的函數的最後一個點。這是爲什麼 ?我在哪裏犯了一個錯誤?
如何在方法中初始化它? –
使用'AttachCurrentThread()'獲得'JNIEnv'指針。本地線程保持連接到VM,直到它調用DetachCurrentThread()來分離它自己。 –
感覺就像我們之前一直在做這件事,我試着引導你最後一次:http://stackoverflow.com/questions/10895826/what-is-wrong-with-this-call-to-the-java-方法/ 10896756#10896756 – maba