標籤:des blog http 使用 os strong io for
在使用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);}