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函数前添加相应变量和处理过程,参考代码如下:
1
HHOOK hKeyHook = NULL;
2
HHOOK hMouseHook = NULL;
3
HWND hWnd;
4
LRESULT CALLBACK MouseProc(
5
int nCode, // hook code
6
WPARAM wParam, // message identifier
7
LPARAM lParam // mouse coordinates
8
)
9
{
10
return 1;
11
}
12![](/Images/OutliningIndicators/None.gif)
13
LRESULT 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
//-------------------------
29
BOOL 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
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)