gpt4 book ai didi

c++ - 带有 WH_KEYBOARD 的 SetWindowsHookEx 卡在循环/队列中

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:07 25 4
gpt4 key购买 nike

我正在尝试使用 dll 注入(inject)来连接记事本。在运行 exe 并 Hook 记事本(形成我可以成功告诉的形式)并按下一些键后,似乎发生的是按键被卡在循环或队列中(记事本没有响应)。在 exe 解除 Hook 后,记事本会响应并且所有按下的键都会出现在文本字段中。

执行

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

HHOOK hHook = NULL;
HWND handle = NULL;
HMODULE dll = NULL;
HOOKPROC address = NULL;
DWORD thread_id = 0;

using namespace std;

int main(){

handle=FindWindow(NULL,L"Untitled - Notepad");
if(handle==NULL){
cout<<"Window not found"<<endl;
getchar();
return 0;
}

thread_id=GetWindowThreadProcessId(handle,NULL);
if(thread_id==0){
cout<<"ID not found"<<endl;
getchar();
return 0;
}

dll = LoadLibrary(TEXT("X:\\qt\\hook\\debug\\hook.dll"));
if(dll==NULL){
cout<<"hook.dll not found"<<endl;
getchar();
return 0;
}

address=(HOOKPROC)GetProcAddress(dll,"CallWndProc@12");
if(address==NULL){
cout<<"Address not found"<<endl;
getchar();
return 0;
}

hHook=SetWindowsHookEx(WH_KEYBOARD,address,dll,thread_id);
if(hHook==NULL){
cout<<"hook was not set"<<endl;
return 0;
}

cout<<"Program successfully hooked"<<endl;
cout<<"Press enter to unhook the function and stop the program"<<endl;
getchar();
UnhookWindowsHookEx(hHook);

return 0;
}

动态链接库

#include "hook.h"
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

extern "C"{
__declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam){

if(nCode<0){
return CallNextHookEx(NULL,nCode,wParam,lParam);
}

ofstream file;
file.open("X:\\qt\\klog\\debug\\function.txt");
file<<"Function keyboard_hook called\n";
file.close();
return CallNextHookEx(NULL,nCode,wParam,lParam);
}
}

BOOL APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved){

switch(Reason) {
case DLL_PROCESS_ATTACH: break;
case DLL_PROCESS_DETACH: break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
}

return TRUE;
}

最佳答案

在 SetWindowsHookEx 和 UnhookWindowsHookEx 之间添加消息循环修复了它

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

关于c++ - 带有 WH_KEYBOARD 的 SetWindowsHookEx 卡在循环/队列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196035/

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