The following java
program calls a native method in C
that should print a message you pressed a key !
if the user presses a key. But i can't see the message as the i press the key.I also check if the function SetWindowsHookEx
returns null but no,it doesn't return null.
Java Code :
package keylogger;
public class TestKeys {
private native void setWinHook();
public static void main(String args[]) {
TestKeys o = new TestKeys();
try {
o.setWinHook();
Thread.sleep(10000);
} catch(Exception exc) {
exc.printStackTrace();
}
}
static {
System.loadLibrary("MyHook");
}
} C Code :
#include <stdio.h>
#include <windows.h>
#include <w32api.h>
#include "keylogger_TestKeys.h"
static HHOOK handleKeyboardHook = NULL;
HINSTANCE hInst = NULL;
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
printf("You pressed a key !\n");
return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
}
void Java_keylogger_TestKeys_setWinHook
(JNIEnv *env, jobject obj) {
hInst = GetModuleHandle(NULL); // include or exclude,i don't see the result
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc,NULL, 0);
if(handleKeyboardHook==NULL) {
printf("Is Null");
} else {
printf("Is not Null");
}
printf("Inside fucntion setWinHook !");
}
/*int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
printf("Hello World !");
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
if(handleKeyboardHook==NULL) {
printf("Is Null");
} else {
printf("Is not Null");
}
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}*/
The only output that I see is Is not NullInside fucntion setWinHook !
Where is the problem ?
What should i do so that this program returns me the message when i press the key.
The only output that I see is : Inside function setWinHook !
Note :
If the above program runs on someone's machine,please mention that.
Output Pic :
I don't see any message on key tapping.Program just exits after 10 seconds without displaying a message.