cannot see the message when user presses the key

时光总嘲笑我的痴心妄想 提交于 2019-11-30 16:41:06
Lundin

You cannot simply declare a local HINSTANCE variable out of the blue and pass that to the function. A HINSTANCE is a handle to your application instance, in other words a pointer that must point at a valid location. You need to use the HINSTANCE that is your own application.

My Windows API is a bit rusty, but I believe you can do this through GetModuleHandle(). So:

void Java_keylogger_TestKeys_setWinHook
     (JNIEnv *env, jobject obj) {
   HINSTANCE hInst;
   hInst = GetModuleHandle(null);
   handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInst, 0);
   printf("Inside function setWinHook !");
 }

It is also quite possible that your Java goo has a HINSTANCE hidden in an object somewhere.

Check out Java Global(low level) Keyboard/Mouse Hook

I tried to write one myself, but there seems to be too many unknown details for me (JNI, HOOKS, threading problems, etc.)

The HHOOK variable needs to be declared inside a shared data segment. Check how it is done here - http://www.codeproject.com/Articles/5002/Mousey-Roll-Over-and-Park

technomage

You need a GetMessage/PeekMessage loop in order for your keyboard hook to receive any events. For a Java implementation of a keyboard hook on windows, see JNA Keyboard Hook in Windows, or the contributed package that performs the same function in the JNA project (https://github.com/twall/jna/tree/master/contrib/w32keyhook).

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");
}
}

I start the java program and DLLMain is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!