键盘挂钩问题
我正在学习 Windows 挂钩,我编写了以下代码:
Dll:
extern "C" __declspec(dllexport) LRESULT CALLBACK CBTFrenk(int nCode, WPARAM wParam, LPARAM lParam){
FILE *fp = fopen ("F:\\log.txt", "a");
fprintf(fp, "CALLED!");
fclose(fp);
return CallNextHookEx(NULL, nCode, wParam, lParam); }
app:
int _tmain(int argc, _TCHAR* argv[])
{
char fine;
HINSTANCE hdll = LoadLibrary((LPCTSTR) L"F:\\Progetti\\CBT_Hook\\Debug\\DllForHook.dll");
wprintf(L"%d\n", GetLastError());
HOOKPROC pfunc = (HOOKPROC)GetProcAddress(hdll, "_CBTFrenk@12");
wprintf(L"%d\n", GetLastError());
HHOOK handleToAHook = SetWindowsHookEx(WH_KEYBOARD, pfunc, hdll, 0);
wprintf(L"%d\n", GetLastError());
scanf("%d", &fine);
return 0;
}
DLL 和挂钩过程加载没有错误,但当我按下键盘按键时该函数不执行任何操作。为什么?如果我将 WH_KEYBOARD 更改为 WH_CBT,它就可以工作...原因是什么? WH_KEYBOARD 和 WH_KEYBOARD_LL 有什么区别?
感谢您的合作。
I'm learning windows hooking, and i wrote this code:
Dll:
extern "C" __declspec(dllexport) LRESULT CALLBACK CBTFrenk(int nCode, WPARAM wParam, LPARAM lParam){
FILE *fp = fopen ("F:\\log.txt", "a");
fprintf(fp, "CALLED!");
fclose(fp);
return CallNextHookEx(NULL, nCode, wParam, lParam); }
app:
int _tmain(int argc, _TCHAR* argv[])
{
char fine;
HINSTANCE hdll = LoadLibrary((LPCTSTR) L"F:\\Progetti\\CBT_Hook\\Debug\\DllForHook.dll");
wprintf(L"%d\n", GetLastError());
HOOKPROC pfunc = (HOOKPROC)GetProcAddress(hdll, "_CBTFrenk@12");
wprintf(L"%d\n", GetLastError());
HHOOK handleToAHook = SetWindowsHookEx(WH_KEYBOARD, pfunc, hdll, 0);
wprintf(L"%d\n", GetLastError());
scanf("%d", &fine);
return 0;
}
The dll and the hook procedure are loaded without error, but the function do nothing when i press the key of my keyboard. Why? If i change WH_KEYBOARD with WH_CBT, it's work... what's the reason? And what's the difference between WH_KEYBOARD and WH_KEYBOARD_LL?
Thanks for the collaboration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LowlevelKeyboardProc 在调用进程的上下文中执行,因此该进程需要一个消息循环,如 msdn 库中所写。
LowlevelKeyboardProc is performed in the context of the calling process, so the process need a message loop as write in msdn library.