Windows SDK 之 Hook的使用

在使用SetWindowsHookEx的过程中遇到的问题

函数原型

HHOOK WINAPI SetWindowsHookEx(
_In_ int idHook,
_In_ HOOKPROC lpfn,
_In_ HINSTANCE hMod,
_In_ DWORD dwThreadId
);

  

WinHook.h

// WinHook.h: interface for the WinHook class.
//
////////////////////////////////////////////////////////////////////// class WinHook
{
public:
WinHook();
BOOL InstallHook(int idHook,HANDLE hInstance,HOOKPROC hookproc,int ThreadID);
BOOL UnHook();
LRESULT CallNextHook(int nCode,WPARAM wParam,LPARAM lParam);
virtual ~WinHook(); private:
HHOOK hHook;
};

  

WinHook.cpp

// WinHook.cpp: implementation of the WinHook class.
//
////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "WinHook.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////////// WinHook::WinHook()
{ } BOOL WinHook::InstallHook(int idHook,HANDLE hInstance,HOOKPROC hookproc,int ThreadID){
if(hookproc == NULL)
return NULL;
hHook = SetWindowsHookEx(idHook,hookproc,(HINSTANCE)hInstance,ThreadID);
return hHook != NULL;
} BOOL WinHook::UnHook(){
if(!hHook)
return false;
UnhookWindowsHookEx(hHook);
hHook = NULL;
return true;
} LRESULT WinHook::CallNextHook(int nCode,WPARAM wParam,LPARAM lParam){
return CallNextHookEx(hHook,nCode,wParam,lParam);
} WinHook::~WinHook()
{
if(hHook)
UnhookWindowsHookEx(hHook);
}

  

其中需要注意到的是回调函数 Hook Procedure

回调函数原型如下:

LRESULT MyHookProc(int nCode,
WPARAM wParam,
LPARAM lParam
);

  

根据MSDN里边说的

nCode [in]

Type: int

Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.

字面意思就是nCode 如果小于0的话 必须通过调用callNextHookex返回值.

如果nCOde 等于HC_ACTIOIN 的话  我们就可以进行处理代码

但是需要知道Hook的时候的消息代码

下面就到了LParam
lParam [in]

Type: LPARAM

A pointer to a CWPRETSTRUCT structure that contains details about the message.

这里说LPARAM 指向了一个CWPRETSTRUCT 里边就包含了Message 的详细信息

下面看操作代码:

startHook and unHook

extern "C"
__declspec(dllexport)
BOOL InstallHook(){
return wh.InstallHook(WH_CALLWNDPROCRET,hThisDLL,(HOOKPROC)MyHookProc,NULL);
} extern "C"
__declspec(dllexport)
BOOL UnHook(){
return wh.UnHook();
}

  

下面是回调函数的操作代码

LRESULT MyHookProc(int nCode,WPARAM wParam,LPARAM lParam){

	if(nCode < 0)
return wh.CallNextHook(nCode,wParam,lParam);
switch (nCode)
{
case HC_ACTION:{ PCWPRETSTRUCT hook_msg = (PCWPRETSTRUCT)lParam;
if(hook_msg){
if(hook_msg->message == WM_SETFOCUS){
// 得到焦点
isAction = true;
}
else if(hook_msg->message == WM_KILLFOCUS){
// 失去焦点
isAction = false;
InvalidateRect(GetParent(hook_msg->hwnd),NULL,true);
}
else if(hook_msg->message == WM_PAINT){
// 绘制客户区
if(isAction){
HDC dc = GetDC(GetParent(hook_msg->hwnd));
TextOut(dc,0,0,_T("123ABCD"),sizeof(_T("123ABCD")));
ReleaseDC(GetParent(hook_msg->hwnd),dc);
}
}
else if(hook_msg->message == WM_NCPAINT){
// 绘制非客户区
HDC dc = GetWindowDC(GetParent(hook_msg->hwnd));
HICON hIcon = LoadIcon((HINSTANCE)hThisDLL,MAKEINTRESOURCE(IDI_ICON1));
DrawIcon(dc,80,0,hIcon);
UpdateWindow(GetParent(hook_msg->hwnd));
ReleaseDC(GetParent(hook_msg->hwnd),dc);
}
} break;
}
default:
break; }
return wh.CallNextHook(nCode,wParam,lParam);
}

  

上一篇:分解成3NF的保持函数依赖的分解算法:


下一篇:Entity Framework Code First 中使用 Fluent API 笔记。