全局键盘钩子仅在线程级别起作用 [英] Global keyboard hook works only in thread level
问题描述
您好。首先抱歉我的英语。
键盘挂钩有问题。我制作了一个挂钩键盘的小程序。它可以在我的计算机上运行,但是如果我在另一台计算机上运行它只能在应用程序的窗口聚焦时才能工作。我在这个问题上找到了很多主题,但没有一个能够帮助我。我用钩子程序制作了一个DLL文件,然后我开始用exe文件挂钩。我还有一个带导出的DEF文件,所以我可以从DLL加载程序,唯一的问题是它在其他计算机上不起作用。我不知道是否有人遇到过这个问题,但我希望有人可以帮助我。
我正在使用Visual Studio 2010和Windows 7 Ultimate x64
并注意:钩子dll只加载到钩子进程。
这是我的源代码:
DLL:
Hi. At first sorry for my English.
I have a problem with keyboard hooking. I made a small program that hooks the keyboard. It works in my computer, but if I run on another computer it works only if the application's window is focused. I found a lot of topics with this question, but none of them helped. I made a DLL file with the hook procedure, and I start hooking with an exe file. I have also a DEF file with exports so I can load the procedures from DLL, the only problem is that it's not works in other computers. I don't know if somebody faced this problem, but I hope that somebody can help me.
I'm using Visual Studio 2010 and Windows 7 Ultimate x64
And a remark: the hook dll is loaded only to hooker process.
Here is my source code:
DLL:
#include <Windows.h>
#pragma data_seg("Shared")
HHOOK hHook = NULL;
#pragma data_seg()
HINSTANCE hInstance = NULL;
void __stdcall Install();
void __stdcall Remove();
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hInstance = (HINSTANCE)hModule;
break;
case DLL_PROCESS_DETACH:
Remove();
break;
}
return TRUE;
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processing data
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
void __stdcall Install()
{
if(hHook == NULL)
{
hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hInstance, 0);
}
}
void __stdcall Remove()
{
if(hHook != NULL)
UnhookWindowsHookEx(hHook);
hHook = NULL;
}
Hooker exe:
Hooker exe:
#include <Windows.h>
typedef void (__stdcall *FuncPtr)(void);
int main()
{
FuncPtr Install;
HMODULE hmodDll = LoadLibrary("DLL.dll");
MSG Msg;
if(hmodDll == NULL)
return -1;
Install = (FuncPtr)GetProcAddress(hmodDll, "Install");
if(Install == NULL)
return -2;
Install();
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
PS
这只是一个实验。
P.S.
This is just an experiment.
推荐答案
请查看我对这个问题的评论并考虑一下。
第一步,请尝试以下方法:而不是打印一些消息,执行以下操作:向扬声器发送蜂鸣声(首先,确保它在正常应用程序中工作),例如:
Please see my comments to the question and think about it.
For first step, please try the following: instead of "printing some messages", do the following: send Beep to the speaker (first, make sure it works in a "normal" application), something like:
#include <windows.h>
//...
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
Beep(440, 400); // A440, 440 Hz, standard la pitch in modern Western
// equal temperament system
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
我不是在开玩笑。我期待你可能会听到两台电脑都发出哔哔声。我已经使用这个简单的技巧解决了一些神秘的不工作问题。
好吧,我可能错了。在这种情况下,我们可以考虑下一步。但请先试试。
这篇关于全局键盘钩子仅在线程级别起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!