gpt4 book ai didi

c++ - 如何向键盘添加钩子(Hook) (HookProc)

转载 作者:可可西里 更新时间:2023-11-01 11:18:17 29 4
gpt4 key购买 nike

我试图做一个简单的键盘记录测试,但我的程序没有按预期工作,我也不知道为什么。

在我的程序中,我有一个低级键盘钩子(Hook)并为其附加了一个简单的过程。该过程只是打开/创建一个文件并写入“Hello World”然后关闭。但是它没有创建文件,可能是因为我的过程不正确或者因为我的 Hook 没有正确建立。

代码:

#include<windows.h>
#include<stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam){
ofstream myfile;
myfile.open ("[PATH]/example.txt");
myfile << "Hello world";//((KBDLLHOOKSTRUCT *)lParam)->vkCode
myfile.close();
return CallNextHookEx(NULL,code,wParam,lParam);
}

int main(void){
HINSTANCE hInst = NULL;
HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInst, 0);
printf("HHOOK is not null: %s\n", (hHook != NULL) ? "True" : "False");
char q;
for (cout << "q to quit\n"; q != 'q'; cin >> q);

printf("Successfully unhooked: %s", UnhookWindowsHookEx(hHook) ? "True" : "False");
}

解决方案 我需要在主函数中添加一个消息循环:

LPMSG Msg;
while(GetMessage(Msg, NULL, 0, 0) > 0)
{
TranslateMessage(Msg);
DispatchMessage(Msg);
}

最佳答案

有两种钩子(Hook):

全局钩子(Hook)
全局钩子(Hook)程序应放在一个单独的 DLL 中,然后在您的主进程中加载​​钩子(Hook) dll 及其钩子(Hook)程序并设置钩子(Hook)。

A global hook monitors messages for all threads in the same desktop as the calling thread. A thread-specific hook monitors messages for only an individual thread. A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module.

就是说:KeyboardProc 进入一个单独的 DLL(例如 myhookdll.dll)。
在你的过程中你做这样的事情:

HOOKPROC hkprc;
static HINSTANCE hhookDLL ;
static HHOOK hhook ;

hhookDLL = LoadLibrary(TEXT("c:\\...\\myhookdll.dll"));
hkprc = (HOOKPROC)GetProcAddress(hhookDLL, "KeyboardProc");

hhook = SetWindowsHookEx(
WH_KEYBOARD,
hkprc,
hhookDLL,
0);

这是一个很好的引用:http://msdn.microsoft.com/en-us/library/windows/desktop/ms644960(v=vs.85).aspx

线程级 Hook
关于这个在线程级 Hook 的特定“WH_KEYBOARD_LL”,它不需要单独的 DLL。

A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL. For information, see Dynamic-Link Libraries.

但是线程级钩子(Hook)需要一个消息泵:

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

这是一个基本的消息循环:

   while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if(bRet == -1)
{
// Handle Error
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

关于c++ - 如何向键盘添加钩子(Hook) (HookProc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070135/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com