What is wrong with this java method call?

I am trying to call a Java method from code. C listens button Escape, Shift, Ctrland then calls a Java method, indicating which key was pressed. The following are snippets that play a role in this.

C Fragment:

mid = (*env)->GetMethodID(env,cls,"callBack","(Ljava/lang/String;)V");
Env = env;
if(called)
    switch(param) {
        case VK_CONTROL:
            printf("Control pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"11"); // calling the java method
            break;
        case VK_SHIFT:
            printf("Shift pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"10"); // calling the java method
            break;
        case VK_ESCAPE:
            printf("Escape pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"1B"); // calling the java method
            break;
        default:
            printf("The default case\n");
            break;
    }

Java Snippet:

public void callBack(String key) {
    String x = KeyEvent.getKeyText(Integer.parseInt(key, 16));
    System.out.println(x);
}

When I run the program and press the key Escape, I get it on the console:

Escape pressed !
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5c8b809a, pid=7588, tid=8088
#
# JRE version: 7.0
# Java VM: Java HotSpot(TM) Client VM (20.0-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# V  [jvm.dll+0x19809a]
#
# An error report file with more information is saved as:
# W:\UnderTest\NetbeansCurrent\KeyLoggerTester\build\classes\hs_err_pid7588.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

I know that I am calling the Java function incorrectly, but I do not know where I am going wrong. Like the exit, it satisfies the case when I press a key Escapeand then an unexpected error appears.

Log File Link

EDIT:

After mavroprovato's answer , I still get the same errors.

I edited this method:

(*Env)->CallVoidMethodA(Env,Obj,mid,(*Env)->NewStringUTF(Env,"1B"));

EDIT:

FULL CODE version 1

COMPLETE CODE version 2

+5
3

JVM , JNIEnv . .

Sun JNI .

, :

JNI_OnLoad. . JavaVM, . (*env)->GetJavaVM initializeJNIVars, .

initializeJNIVars obj, Obj = (*env)->NewGlobalRef(obj).

LowLevelKeyboardProc env:

AttachCurrentThread(JavaVM *jvm, JNIEnv &env, NULL);


Edit

, , , , , . NB: , , , .

:

static JavaVM *javaVM = NULL;
static jmethodID callbackMethod = NULL;
static jobject callbackObject = NULL;

cls, mid, env obj .

JNI_OnLoad, JavaVM:

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
    JNIEnv *env = 0;

    if ((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4)) {
        return JNI_ERR;
    }

    javaVM = jvm;

    return JNI_VERSION_1_4;
}

initializeJNIVars :

void Java_keylogger_TestKeys_initializeJNIVars(JNIEnv *env, jobject obj) {
    jclass cls = (*env)->GetObjectClass(env,obj);
    callbackMethod = (*env)->GetMethodID(env, cls, "callBack", "(Ljava/lang/String;)V");
    callbackObject = (*env)->NewGlobalRef(env, obj);
    if(cls == NULL || callbackMethod == NULL) {
        printf("One of them is null \n");
    }
    called = TRUE;
}

, , LowLoevelKeyboardProc :

...
WPARAM param = kbhook->vkCode;

JNIEnv *env;
jint rs = (*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
if (rs != JNI_OK) {
    return NULL; // Or something appropriate...
}
...

    case VK_ESCAPE:
        printf("Escape pressed !\n");
        jstring message = (*env)->NewStringUTF(env, "1B");
        (*env)->CallVoidMethod(env, callbackObject, callbackMethod, message);
        break;
...

unregisterWinHook , GC'd.

...
(*env)->DeleteGlobalRef(env, callbackObject);

.

+4

, java-, String char*. NewStringUTF.

+3

I think this is because the UAC feature is enabled on your operating system. This was a bug for Java 6. Read this for more details .

The reason I say this is because the event for the evacuation key is triggered correctly, and the problem only starts after calling the java method.

0
source

All Articles