带有WH_KEYBOARD的SetWindowsHookEx卡在循环/队列中
SetWindowsHookEx with WH_KEYBOARD stuck in a loop/queue
我正在尝试用dll注入挂接记事本。在exe运行并挂接记事本(我可以成功地告诉你)并且按下一些键之后,似乎发生了按键卡在循环或队列中(记事本没有响应)。exe解除挂起后,记事本会做出响应,所有按下的键都会显示在文本字段中。
exe
#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;
}
dll
#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 calledn";
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和UnookWindowsHookEX之间添加消息循环修复了
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
相关文章:
- 显示循环双端队列的功能
- EnQueue 方法在循环队列中未正确返回C++?
- 实现循环阵列队列
- C++ 数据结构队列:使用 for 循环查找队列中最大的元素
- 优先级队列(使用 pairs<int,int> ) 根据 for 循环迭代器的顶级元素
- Qt的事件循环线程是安全的还是原子的?处理"队列连接"时如何同步?
- 使用循环队列和迭代器自动换行
- 为什么在使用队列处理扫雷中的相邻单元格时会出现无限循环?
- 在队列上的最后一个操作循环中的错误
- C++ 长度为 100 的循环队列类
- 循环队列和循环链表
- 查找循环队列中的元素数
- 复制构造函数不适用于循环队列
- 创建固定大小队列的向量(提升循环队列)
- 增加C++循环队列的容量
- 循环队列中的c++模板专用化
- 带有WH_KEYBOARD的SetWindowsHookEx卡在循环/队列中
- 循环队列中的pop()操作.我该如何移除物品呢?
- 创建动态循环队列,元素输出正常,但程序崩溃
- 仅更新索引的循环队列