HOOK笔记[1]
HOOK分为内部HOOK和全局HOOK
一、内部HOOK
1、设置内部HOOK
HHOOK SetWindowsHookEx(
int idHook, // hook type
HOOKPROC lpfn, // hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // thread identifier
);
其中hMod参数设为NULL,dwThreadId 参数设为调用HOOK的实例句柄;GetCurrentThreadId()可以得到当前实例的句柄。
2、编写HOOK过程
将HOOK过程名传递给lpfn参数;鼠标HOOK过程可以如此写:
LRESULT CALLBACK MouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
return 1;//表示我们已经对鼠标消息进行了处理,操作系统将不会再把鼠标消息发送给“窗口”;
}
3、释放HOOK句柄
在退出程序是调用UnhookWindowsHookEx释放HOOK句柄,如:
UnhookWindowsHookEx(hMouseHook);
4、参考示例
这是一个简单的对话框程序,只需在OnInitDialog函数中设置内部HOOK;并在OnInitDialog函数前添加相应变量和处理过程,参考代码如下:
一、内部HOOK
1、设置内部HOOK
HHOOK SetWindowsHookEx(
int idHook, // hook type
HOOKPROC lpfn, // hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // thread identifier
);
其中hMod参数设为NULL,dwThreadId 参数设为调用HOOK的实例句柄;GetCurrentThreadId()可以得到当前实例的句柄。
2、编写HOOK过程
将HOOK过程名传递给lpfn参数;鼠标HOOK过程可以如此写:
LRESULT CALLBACK MouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
return 1;//表示我们已经对鼠标消息进行了处理,操作系统将不会再把鼠标消息发送给“窗口”;
}
3、释放HOOK句柄
在退出程序是调用UnhookWindowsHookEx释放HOOK句柄,如:
UnhookWindowsHookEx(hMouseHook);
4、参考示例
这是一个简单的对话框程序,只需在OnInitDialog函数中设置内部HOOK;并在OnInitDialog函数前添加相应变量和处理过程,参考代码如下:
1HHOOK hKeyHook = NULL;
2HHOOK hMouseHook = NULL;
3HWND hWnd;
4LRESULT CALLBACK MouseProc(
5 int nCode, // hook code
6 WPARAM wParam, // message identifier
7 LPARAM lParam // mouse coordinates
8)
9{
10 return 1;
11}
12
13LRESULT CALLBACK KeyboardProc(
14 int nCode, // hook code
15 WPARAM wParam, // message identifier
16 LPARAM lParam // mouse coordinates
17)
18{
19 if(VK_F4==wParam && (lParam>>29 & 1))//ALT+F4
20 {
21 ::SendMessage(hWnd,WM_CLOSE,0,0);
22 UnhookWindowsHookEx(hKeyHook);
23 UnhookWindowsHookEx(hMouseHook);
24 }
25 else
26 return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
27}
28//-------------------------
29BOOL CInnerHookDlg::OnInitDialog()
30{
31 .
32 // TODO: Add extra initialization here
33 //设置钩子
34 hWnd = m_hWnd; / /在SendMessage中要用到该窗口句柄
35 hMouseHook=SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());
36 hKeyHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());
37 return TRUE; // return TRUE unless you set the focus to a control
38}
2HHOOK hMouseHook = NULL;
3HWND hWnd;
4LRESULT CALLBACK MouseProc(
5 int nCode, // hook code
6 WPARAM wParam, // message identifier
7 LPARAM lParam // mouse coordinates
8)
9{
10 return 1;
11}
12
13LRESULT CALLBACK KeyboardProc(
14 int nCode, // hook code
15 WPARAM wParam, // message identifier
16 LPARAM lParam // mouse coordinates
17)
18{
19 if(VK_F4==wParam && (lParam>>29 & 1))//ALT+F4
20 {
21 ::SendMessage(hWnd,WM_CLOSE,0,0);
22 UnhookWindowsHookEx(hKeyHook);
23 UnhookWindowsHookEx(hMouseHook);
24 }
25 else
26 return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
27}
28//-------------------------
29BOOL CInnerHookDlg::OnInitDialog()
30{
31 .
32 // TODO: Add extra initialization here
33 //设置钩子
34 hWnd = m_hWnd; / /在SendMessage中要用到该窗口句柄
35 hMouseHook=SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());
36 hKeyHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());
37 return TRUE; // return TRUE unless you set the focus to a control
38}