English 中文(简体)
无法在用户按下密钥时看到此消息
原标题:cannot see the message when user presses the key

以下 java 程序在 C 中呼叫本地方法,该方法应该打印一条消息 ,如果用户按下密钥,你按下密钥! 。但是,我看不到消息是i按下密钥。 我还会检查函数 SetWindowsHookEx 是否返回无效,但没有,它不会返回无效。

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 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 !
");
  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 fucntion 设置 WinHook!

问题在哪里?

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 !

注:

如果上面的程序运行在某人的S机器上 请提及这一点

Exput Pic:

""https://i.sstatic.net/39nY8.jpg" alt="此处输入图像描述"/"

我看不到任何关于密钥窃听的信息。 程序在10秒后退出, 没有显示消息 。

最佳回答

有更好的方法可以执行此操作 。 这里 < code> DllMain 被多次调用, 一旦创建了一条线条, 这对我来说似乎不正确 。 我不确定这是否合法! C 代码启动一条新线以执行密钥抓取器 。

C代码:

#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
");
    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
",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 代码:

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 程序, 并调用 < code> DLLMain

问题回答

您不能简单地将本地 HINSTENCE 变量从蓝色中声明出来, 并将其传递到函数。 HINSTENCE 是您应用程序实例的手柄, 换句话说, 指针指向一个有效位置。 您需要使用您自己的应用程序 HINSTENCE 。

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

你的爪哇谷也很有可能 藏在某处的物体里

校对"http://ksquared.de/blog/2011/07/java-global-system-hook/"rel="nofollow">Java Global(低级别)键盘/Mouse Hook

我试图自己写一个,但对我来说似乎有太多未知的细节(JNI、HOOKS、针线问题等等)。

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

您需要一个 < code> GetMessage/ PeekMessage 环, 以便您的键盘钩能够接收任何事件。 关于在窗口上执行键盘钩的 Java, 见 < a href="https://stackoverflow.com/ questions/3078646/jna-keeyboard-hook-in-windows > > > JNA Keyboard Hook in Window , 或者在 JNA 项目中履行相同功能的组合 (https://github.com/twal/jna/tree/master/contrib/w32keyhook )。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...