Platform SDK: Interprocess Communications
Monitoring System Events
The following example uses a variety of thread-specific hook procedures to monitor the system for events affecting a thread It demonstrates how to process events for the following types of hook procedures.:
WH_CallWndProc
WH_CBT
WH_Debug
WH_GetMessage
WH_KEYBOARD
WH_MOUSE
WH_MSGFILTER
The user can install and remove a hook procedure by using the menu. When a hook procedure is installed and an event that is monitored by the procedure occurs, the procedure writes information about the event to the client area of the application's main window.
#define NumHooks 7
// Global Variables
Typedef struct_myhookdata
{
int NTYPE;
HookProc HKPRC;
Hhook hhook;
} Myhookdata;
MyhookData myhookdata [numhooks];
LResult WinApi MainwndProc (HWND HWndmain, Uint UMSG, WPARAM WPARAM,
LParam LPARAM)
{
Static Bool Afhooks [NumHooks];
Int index;
Static Hmenu Hmenu;
Switch (UMSG)
{
Case WM_CREATE:
// save the menu handle.
HMENU = GetMenu (hwndmain);
// Initialize Structures with hook data. The menu-item
// Identifiers Are Defined As 0 THROUGH 6 in The
//Header file. They can be used to Identify Array
// Elements Both Here and During the WM_COMMAND
// Message.
MyHOOKDATA [IDM_CALLWNDPROC] .ntype = wh_callwndproc;
MyHOOKDATA [IDM_CALLWNDPROC] .hkprc = CallwndProc;
MyHOOKDATA [IDM_CBT] .ntype = wh_cbt;
MyHOOKDATA [IDM_CBT] .hkprc = CBTPROC;
MyHOOKDATA [IDM_DEBUG] .ntype = wh_debug;
MyHOOKDATA [IDM_DEBUG] .hkprc = debugproc;
MyHOOKDATA [IDM_GETMESSAGE] .ntype = wh_getimentage;
MyHOOKDATA [IDM_GETMESSAGE] .hkprc = getMsgProc;
Myhookdata [IDM_KEYBOARD] .ntype = wh_keyboard;
Myhookdata [IDM_KEYBOARD] .hkprc = keyboardproc; myhookdata [idm_mouse] .ntype = wh_mouse;
MyHOOKDATA [IDM_MOUSE] .hkprc = mouseproc;
MyHOOKDATA [IDM_MSGFILTER] .ntype = wh_msgfilter;
MyHOOKDATA [IDM_MSGFILTER] .hkprc = messageproc;
// Initialize All Flags in the array to false.
MEMSET (Afhooks, False, Sizeof (Afhooks));
Return 0;
Case WM_COMMAND:
Switch (loword (wparam))
{
// The user selected a hook command from the menu.
Case IDM_CallWndProc:
Case IDM_CBT:
Case IDM_Debug:
Case IDM_GetMessage:
Case IDM_Keyboard:
Case IDM_MOUSE:
CASE IDM_MSGFILTER:
// use the menu-item identifier as an index
// INTO The Array Of Structures with hook data.
INDEX = loword (wparam);
// if the selected type of hook procedure isn '
// installed yet, install it and check the
// Associated Menu Item.
IF (! Afhooks [INDEX])
{
MyhookData [index] .hook = setwindowshookex
Myhookdata [index] .ntype,
Myhookdata [Index] .hkprc,
NULL, GetCurrentThreadId ());
Checkmenuitem (Hmenu, INDEX,
MF_BYCOMMAND | MF_CHECKED);
Afhooks [Index] = TRUE
}
// if The successd type of hook procedure is
// Already Installed, Remove It And Remove The
// Check Mark from The Associated Menu Item.
Else
{
UnHookWindowshookex (MyHOOKDATA [INDEX] .hook;
Checkmenuitem (Hmenu, INDEX,
MF_BYCOMMAND | MF_UNCHECKED);
Afhooks [index] = false;
}
DEFAULT:
Return (DEFWINDOWPROC (HWndmain, UMSG, WPARAM,
lparam);
}
Break;
//
// Process Other Messages.
//
DEFAULT:
Return DefWindowProc (HWndmain, UMSG, WPARAM, LPARAM);
}
Return NULL;
}
/ ************************************************** ****************
Wh_CallWndProc Hook Procedure
*********************************************************** ************* /
Lresult WinAPI CallWndProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM) {
Char szcwpbuf [256];
Char szmsg [16];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (MyHOOKDATA [CallWndProc] .hook, ncode,
WPARAM, LPARAM;
// Call An Application-Defined Function That Converts a Message
// constant to a string and copies it to a buffer.
LookuptheMessage ((PMSG) LPARAM, SZMSG;
HDC = Getdc (hwndmain);
Switch (ncode)
{
Case HC_Action:
CCH = WSPRINTF (SZCWPBUF,
"CallWndProc - TSK:% LD, MSG:% S,% D Times",
WPARAM, SZMSG, C );
Textout (HDC, 2, 15, SZCWPBUF, CCH);
Break;
DEFAULT:
Break;
}
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (MyHOOKDATA [CallWndProc] .hook, ncode,
WPARAM, LPARAM;
}
/ ************************************************** ****************
Wh_getMessage Hook ProCedure
*********************************************************** ************* /
LResult Callback GetMsgProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szmsgbuf [256];
Char szrem [16];
Char szmsg [16];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (MyHOOKDATA [GetMessage] .hook, ncode,
WPARAM, LPARAM;
Switch (ncode)
{
Case HC_Action:
Switch (WPARAM)
{
Case PM_Remove:
LSTRCPY (Szrem, "PM_Remove");
Break;
Case PM_NOREMOVE:
LSTRCPY (Szrem, "PM_NOREMOVE");
Break;
DEFAULT:
LSTRCPY (Szrem, "unknown");
Break;
}
// Call An Application-Defined Function That Converts A
// Message Constant to a string and copies it TO A
// buffer.
LookuptheMessage ((PMSG) LPARAM, SZMSG;
HDC = Getdc (hwndmain);
CCH = WSPRINTF (Szmsgbuf,
"GetMessage - WPARAM:% S, MSG:% S,% D Times", Szrem, SZMSG, C ;
Textout (HDC, 2, 35, Szmsgbuf, CCH);
Break;
DEFAULT:
Break;
}
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (MyHOOKDATA [GetMessage] .hook, ncode,
WPARAM, LPARAM;
}
/ ************************************************** ****************
WH_Debug hook procedure
*********************************************************** ************* /
LResult Callback DebugProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szbuf [128];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (MyHOOKDATA [Debug] .hook, ncode,
WPARAM, LPARAM;
HDC = Getdc (hwndmain);
Switch (ncode)
{
Case HC_Action:
CCH = WSPRINTF (SZBUF,
"Debug - NCODE:% D, TSK:% LD,% D Times",
NCODE, WPARAM, C );
Textout (HDC, 2, 55, SZBUF, CCH);
Break;
DEFAULT:
Break;
}
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (MyHOOKDATA [Debug] .hook, ncode, wparam,
lparam;
}
/ ************************************************** ****************
WH_CBT HOOK Procedure
*********************************************************** ************* /
LResult Callback CBTProc (Int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szbuf [128];
Char szcode [128];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (Myhookdata [CBT] .hook, ncode, wparam,
lparam;
HDC = Getdc (hwndmain);
Switch (ncode)
{
Case hcbt_activate:
LSTRCPY (Szcode, "HCBT_ACTIVATE");
Break;
Case HCBT_CLICKSKIPPED:
LSTRCPY (Szcode, "HCBT_CLICKSKIPPED");
Break;
Case HCBT_CREATEWND:
LSTRCPY (Szcode, "HCBT_CREATEWND");
Break;
Case HCBT_DESTROYWND:
LSTRCPY (Szcode, "HCBT_DESTROYWND"); Break;
Case HCBT_KEYSKIPPED:
LSTRCPY (Szcode, "HCBT_KEYSKIPPED");
Break;
Case HCBT_MINMAX:
LSTRCPY (Szcode, "HCBT_MINMAX");
Break;
Case HCBT_MOVESIZE:
LSTRCPY (Szcode, "HCBT_MOVESIZE");
Break;
Case HCBT_QS:
LSTRCPY (Szcode, "HCBT_QS");
Break;
Case HCBT_SETFOCUS:
LSTRCPY (Szcode, "HCBT_SETFOCUS");
Break;
Case HCBT_SYSCOMMAND:
LSTRCPY (Szcode, "HCBT_SYSCOMMAND");
Break;
DEFAULT:
LSTRCPY (Szcode, "Unknown");
Break;
}
CCH = WSPRINTF (SZBUF, "CBT - NCODE:% S, TSK:% LD,% D Times",
Szcode, WPARAM, C ;
Textout (HDC, 2, 75, SZBUF, CCH);
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (Myhookdata [CBT] .hook, ncode, wparam,
lparam;
}
/ ************************************************** ****************
Wh_mouse hook procedure
*********************************************************** ************* /
LResult Callback MouseProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szbuf [128];
Char szmsg [16];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // do not process the message
Return CallNexthookex (MyHOOKDATA [Mouse] .hook, ncode,
WPARAM, LPARAM;
// Call An Application-Defined Function That Converts a Message
// constant to a string and copies it to a buffer.
LookuptheMessage ((PMSG) LPARAM, SZMSG;
HDC = Getdc (hwndmain);
CCH = WSPRINTF (SZBUF,
"Mouse - NCODE:% D, MSG:% S, X:% D, Y:% D,% D Times",
NCODE, SZMSG, LOWORD (LPARAM), HIWORD (LPARAM), C );
Textout (HDC, 2, 95, SZBUF, CCH);
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (MyHOOKDATA [Mouse] .hook, ncode, wparam,
lparam;
}
/ ************************************************** ****************
WH_Keyboard Hook Procedure
*********************************************************** ************* /
Lresult Callback KeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szbuf [128];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (MyHOOKDATA [Keyboard] .hook, ncode,
WPARAM, LPARAM;
HDC = Getdc (hwndmain);
CCH = WSPRINTF (SZBUF, "Keyboard - NCODE:% D, VK:% D,% D Times",
NCODE, WPARAM, C );
Textout (HDC, 2, 115, SZBUF, CCH);
ReleaseDC (HWndmain, HDC);
Return CallNexthookex (MyHOOKDATA [Keyboard] .hook, ncode, wparam,
lparam;
}
/ ************************************************** ****************
WH_MSGFilter Hook Procedure
*********************************************************** ************* /
LResult Callback MessageProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Char szbuf [128];
Char szmsg [16];
Char szcode [32];
HDC HDC;
Static int C = 0;
INT CCH;
IF (ncode <0) // Do Not Process Message
Return CallNexthookex (MyHOOKDATA [MSGFilter] .hook, ncode,
WPARAM, LPARAM;
Switch (ncode)
{
Case msgf_dialogbox:
LSTRCPY (Szcode, "MSGF_DIALOGBOX");
Break;
Case MSGF_Menu:
LSTRCPY (Szcode, "MSGF_MENU");
Break;
Case msgf_scrollbar:
LSTRCPY (Szcode, "MSGF_Scrollbar");
Break;
DEFAULT:
WSPRINTF (Szcode, "Unknown:% D", NCODE);
Break;
}
// Call An Application-Defined Function That Converts a Message
// constant to a string and copies it to a buffer.
LookuptheMessage ((PMSG) LPARAM, SZMSG;
HDC = Getdc (hwndmain);
CCH = WSPRINTF (SZBUF,
"MSGFilter NCODE:% S, MSG:% S,% D Times",
Szcode, SZMSG, C ;
Textout (HDC, 2, 135, SZBUF, CCH);
ReleaseDC (HWndmain, HDC);
Return CallNexthooKex (MyHOKDATA [MSGfilter] .hook, ncode, wparam, lparam);
}
Built on
Thursday, October 12, 2000