使用 KeyboardProc / SetWindowsHookEx 从注入的 DLL 中挂钩键盘
注意:我正在使用纯 C 语言。不是 C++,也不是 C#。
我正在开发一个模组。我已经编写了一个可用的 DLL 注入器,以及要注入的 DLL。除了用户输入之外,一切都很顺利。
我希望能够使用热键,因此我尝试使用 SetWindowsHookEx 设置键盘钩子。 以下是我的回调函数:
LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
printf("key touched\n");
if (wParam == VK_F5)
{
keyEvent = VK_F5;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
这就是我的设置方式:
HHOOK kbHookHandle = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)keyboardHook, NULL, GetCurrentThreadId());
if (kbHookHandle != NULL)
{
printf("keyboard hook successful!\n");
}
else
{
printf("keyboard hook failed!\n");
}
据我所知,挂钩设置得很好(我曾经遇到过参数无效的问题,但通过使用 GetCurrentThreadID 解决了这个问题)。它返回一个不为 NULL 的句柄。
但每当我按下一个键时,就没有任何输出。
进一步澄清: 上面的代码来自注入的DLL。因此它实际上“属于”游戏过程。我已经使用 AllocConsole 分配了一个控制台,用于打印调试消息。
我做错了什么?
编辑: 为了澄清(甚至更多):列出的代码来自注入 DLL。这不是我用来注入 DLL 的方法 - 我编写了一个单独的(工作!)程序来做到这一点。
我使用 printf() 令一些人感到惊讶,因为考虑到我从主机进程内部调用它,它不会显示。 是的,我确实从主机进程内部调用它,但这不是问题,因为我已经分配了一个工作控制台。我使用的方法与此处提到的方法非常相似
编辑2: 我不是在问为什么 printf() 不起作用(因为它是),我是在问为什么这个键盘钩子不起作用。
Note: I am working in plain C. Not C++, not C#.
I am working on a mod. I've already written a working DLL-injector, as well as the DLL to be injected. Everything is going well, apart from the userinput.
I want to be able to use hotkeys, so I tried to setup a keyboardhook using SetWindowsHookEx.
The following is my callback function:
LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
printf("key touched\n");
if (wParam == VK_F5)
{
keyEvent = VK_F5;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
And this is how I set it up:
HHOOK kbHookHandle = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)keyboardHook, NULL, GetCurrentThreadId());
if (kbHookHandle != NULL)
{
printf("keyboard hook successful!\n");
}
else
{
printf("keyboard hook failed!\n");
}
As far as I can tell, the hook gets setup well (I used to have a problem with an invalid parameter, but fixed that by using GetCurrentThreadID). It returns a handle which is not NULL.
But whenever I press a key, there is no output.
To further clarify:
The code above is from the injected DLL. So it effectively 'belongs' to the game process. I have allocated a console using AllocConsole, to print debug messages to.
What am I doing wrong?
EDIT:
To clarify (even more): the listed code is from the injected DLL. It is not the approach I use to inject the DLL - I wrote a seperate (working!) program to do just that.
It surprises some that I use printf(), since that wouldn't show up, considering I call it from inside the host process. Yes, I do call it from inside the host process, but that is not an issue, because I have already allocated a working console. I used an approach very similar to the one mentioned here
EDIT2:
I am not asking why printf() isn't working (because it is), I am asking why this keyboardhook isn't working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我在 NoQuake ID Tech 游戏最小化器。希望有帮助。
Here's how I detect key down and key up in my NoQuake ID Tech games minimizer. Hope it helps.