以下java
程序调用本机方法,如果用户按下某个键,C
该方法应打印一条消息。you pressed a key !
但是当我按下键时我看不到消息。我还检查函数是否SetWindowsHookEx
返回 null 但不,它不返回 null。
Java 代码:
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 代码:
#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;
}*/
我看到的唯一输出是Is not NullInside fucntion setWinHook !
哪里有问题 ?
我应该怎么做才能让这个程序在我按下键时返回消息。我看到的唯一输出是:Inside function setWinHook !
笔记 :
如果上述程序在某人的机器上运行,请提及。
输出图片:
我在按键时没有看到任何消息。程序在 10 秒后退出而不显示消息。