C++ hook 问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ hook 问题相关的知识,希望对你有一定的参考价值。
检测键盘,有按键就cout<<“按键了”。
应该是怎么个流程??
要不要自己写个DLL?然后用程序加载或注入?
我知道用什么函数,但是返回的句柄老是0。
#include <stdio.h>
#include <tchar.h>
// function declaration.
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam );
int main()
// Retrieve the applications instance
HINSTANCE appInstance = GetModuleHandle(NULL);
// Set a global Windows Hook to capture keystrokes.
SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, appInstance, 0 );
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
TranslateMessage(&msg);
DispatchMessage(&msg);
return 0;
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
// Declare our pointer to the KBDLLHOOKSTRUCT
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
switch( wParam )
case WM_KEYUP: // When the key has been pressed and released
switch( pKeyBoard->vkCode ) // Check to see what key has been pushed
case VK_RETURN: // The return/enter key has been pressed
DWORD timestamp = pKeyBoard->time; // This shows our timestamp when the key was pushed.
printf("Enter Key Pressed %u", timestamp ); // Show us when the key has been pushed
break;
default:
return CallNextHookEx( NULL, nCode, wParam, lParam );
return 0;
参考技术A 如果是Hook全局的而不是某个特定的进程则需要将Hook过程编写为一个DLL,以便让任何程序都可以加载它来获取Hook过程函数。至于你说的返回的句柄是0,我记得hook键盘按键的钩子获取到的值是存在wParam里面的。返回值当然不会有结果。
以上是关于C++ hook 问题的主要内容,如果未能解决你的问题,请参考以下文章
使用 Windows Hooks 拦截鼠标点击我的应用程序 c++
C++ HOOK 指定进程的指定 API(MessageBoxA 为例)(最简单)