不能看到该消息时,用户presses关键 [英] cannot see the message when user presses the key

查看:172
本文介绍了不能看到该消息时,用户presses关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的的Java 程序调用 C 本地方法应该打印一条消息您pressed的关键!如果用户presses的关键。但我无法看到消息作为我preSS的key.I还检查功能和SetWindowsHookEx 返回null,但没有,它没有返回null。

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;
}*/

这是我看到的唯一输出的不NullInside温控功能setWinHook!

The only output that I see is Is not NullInside fucntion setWinHook !

问题出在哪里?

我应该做的,这样这个程序返回我的短信时,我preSS的关键。
我看到的唯一输出是:<!code>内部功能setWinHook

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 !

注意:

如果上面的程序别人的机器上运行,请提及。

If the above program runs on someone's machine,please mention that.

输出图:

我不明白在关键tapping.Program的任何消息在10秒后只退出而不显示消息。

I don't see any message on key tapping.Program just exits after 10 seconds without displaying a message.

推荐答案

有是肯定要实现这一种更好的方式。在这里,的DllMain 被多次调用,一旦一个线程被创建,这似乎不正确的我。我不敢肯定这是否是合法的!在C code开头一个新的线程来实现keycatcher。

There is for sure a better way to implement this. Here DllMain is called multiple times once a thread has been created,that doesn't seem right to me.I am not sure if this is legal ! The C Code starts a new thread to implement the keycatcher.

C code:

#include <stdio.h>
#include <windows.h>
#include <w32api.h>
#include "keylogger_TestKeys.h"

static HHOOK handleKeyboardHook = NULL;
HINSTANCE hInst = NULL;
static DWORD hookThreadId = 0;
static HANDLE hookThreadHandle = NULL;
BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved);
static int i = 0;

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {

  printf("You pressed the key !");
  return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
}


BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
 if(hookThreadHandle==NULL) {
    printf("hookThreadHandle is NULL\n");
    LPTHREAD_START_ROUTINE lpStartAddress = &installHook;
    hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &hookThreadId);
 }
}

BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
// printf("From installHook : %u",fwdReason);
printf("count : %d\n",i++);
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
MSG msg;

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

void Java_keylogger_TestKeys_unregisterWinHook
 (JNIEnv *env, jobject obj) {
 // should have stopped the thread before unhooking
 if(handleKeyboardHook != NULL) {
    UnhookWindowsHookEx(handleKeyboardHook);
 }
}

void Java_keylogger_TestKeys_stopNativeThread // stop the native thread
  (JNIEnv *env, jobject obj) {
    PostThreadMessage(hookThreadId, WM_QUIT, (WPARAM) NULL, (LPARAM) NULL);
    WaitForSingleObject(hookThreadHandle, 5000);
}

的Java code:

package keylogger;

public class TestKeys {
private static int i = 0;
private native void setWinHook();
private native void unregisterWinHook();
private native void createWinThread();
private native void stopNativeThread();

public static void main(String args[]) {
    TestKeys o = new TestKeys();
    try {
        Thread.sleep(5000);
    }catch(Exception exc) {
        exc.printStackTrace();
    }
   o.stopNativeThread();
   o.unregisterWinHook();
   System.out.println("Native thread stopped and Hook unregistered !");

   try {
        Thread.sleep(3000); // Now you won't see the message : you pressed the key 
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

static {
    System.loadLibrary("MyHook");
}
}

我启动java程序和的DLLMain 被调用。

这篇关于不能看到该消息时,用户presses关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆