Windows SDK 之 Hook的使用

來源:互聯網
上載者:User

標籤: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);}

  

 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.