我很难弄清楚为什么我无法让我的钩子在java应用程序中工作..
我创建了一个C ++ WINAPI窗口。我加载了我的“hook”dll,每当我向应用程序发送虚假输入时,它都会提升一个标志。
但是,如果我对Java应用程序执行相同操作,它根本不起作用!
我的dll看起来像这样:
#include <windows.h>
HINSTANCE hInstance = nullptr;
HHOOK MouseHook = {0}; /** Structure for hooking the mouse **/
HHOOK KeyboardHook = {0}; /** Structure for hooking the keyboard **/
LRESULT __stdcall MouseHookProc(int Code, WPARAM wParam, LPARAM lParam)
{
if (Code == HC_ACTION)
{
MSLLHOOKSTRUCT* Info = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
if ((Info->flags & LLMHF_INJECTED) == LLMHF_INJECTED)
{
MessageBox(NULL, "DETECTED Mouse", "", 0);
}
}
return CallNextHookEx(MouseHook, Code, wParam, lParam);
}
LRESULT __stdcall KeyboardHookProc(int Code, WPARAM wParam, LPARAM lParam)
{
if (Code == HC_ACTION)
{
KBDLLHOOKSTRUCT* Info = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
if ((Info->flags & LLKHF_INJECTED) == LLKHF_INJECTED)
{
MessageBox(NULL, "DETECTED Keyboard", "", 0);
}
}
return CallNextHookEx(KeyboardHook, Code, wParam, lParam);
}
extern "C" __declspec(dllexport) bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
hInstance = hinstDLL;
SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInstance, 0);
SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hInstance, 0);
}
break;
case DLL_PROCESS_DETACH:
{
UnhookWindowsHookEx(KeyboardHook);
UnhookWindowsHookEx(MouseHook);
}
break;
}
return true;
}
然后我使用以下WM_CREATE
块创建了一个C ++ WINAPI应用程序:
case WM_CREATE:
LoadLibrary("JavaHook.dll");
mouse_event(MOUSEEVENTF_LEFTDOWN, 100, 100, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,100, 100,0,0);
break;
弹出MouseHook的MessageBox。这是预期的行为。
然而,对于Java,我做了:
public class HookTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.loadLibrary("JavaHook");
}
}
然后我在应用程序上使用了SendInput,如下所示:
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = 100 * (65536 / GetSystemMetrics(SM_CXSCREEN)); //x being coord in pixels
input.mi.dy = 100 * (65536 / GetSystemMetrics(SM_CYSCREEN)); //y being coord in pixels
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
弹出和NOTHING。我尝试使用mouse_event
和keybd_event
,但仍然没有。我尝试使用Java鼠标监听器和键盘监听器以及伪造事件..仍然没有......
钩子肯定会加载,因为我可以看到鼠标滞后3秒钟才能移动它。还有一个弹出窗口,以便在加载时确认它。
但是,我仍然无法弄清楚为什么它在Java应用程序上不起作用。任何想法如何让他们工作。或者是否有另一种方法来为Java做优选来自C / C ++?如果没有,有没有办法在Java中做到这一点?
答案 0 :(得分:1)
您可能希望阅读SetWindowsHookEx文档的评论。
具体来说,安装全局钩子的线程应该继续处理Windows消息,以便允许32位和64位应用程序在挂钩应用程序的上下文中运行钩子代码。
所以,我建议你从DllMain创建一个单独的线程。该线程将安装一个钩子,然后运行到GetMessage / ProcessMessage循环:http://pastebin.com/JmMvMX78