Use of the Windows SDK hooks

Source: Internet
Author: User

Problems encountered in the process of using SetWindowsHookEx

Function prototypes

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 ();p rivate: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);}

  

One of the things to note is the callback function Hook Procedure

The callback function is prototyped as follows:

LRESULT myhookproc (int nCode,    WPARAM WPARAM,    LPARAM LPARAM);

  

According to the MSDN inside,

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 with Out further processing and should return the value returned by CallNextHookEx.

The literal meaning is that ncode must return a value by calling CallNextHookEx if it is less than 0.

If Ncode equals Hc_actioin, we can handle the code.

But need to know the message code when the hook

Here we go, lparam.
LParam [In]

Type: LPARAM

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

It says here that lparam points to a cwpretstruct with the details of the message contained inside it.

Let's look at the operation code:

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 ();}

  

Here is the action code for the callback function

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) {//gets focus isaction = true;} else if (hook_msg->message = = Wm_killfocus) {//loses focus isaction = False;invalidaterect (GetParent (Hook_msg->hwnd), Null,true);} else if (hook_msg->message = = WM_PAINT) {//Draw client area 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) {//Draw non-client area hdc DC = GETWINDOWDC (GetParent (Hook_msg->hwnd)); Hicon Hicon = LoadIcon ((hinstance) Hthisdll,makeintresource (idi_icon1));D Rawicon (Dc,80,0,hicon); UpdateWindow (GetParent (Hook_msg->hwnd)); ReleaseDC (GetParent (Hook_msg->hwnd), DC);}} break;} Default:break;} return WH. Callnexthook (Ncode,wparam,lparam);}

  



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.