This function is part of the Windows message processing mechanism, by setting "hook", application can
To filter all messages, events in the system level, access messages that cannot be accessed under normal conditions. Of course, this
What does it take to pay a certain price. Because of the more than one processing process, system performance will be sure
Impact, so everyone uses "hooks" when necessary, and deletes them in time.
Let us first take a look at how the hook function is installed, calls, and deletes. Application is usually called setwin
DOWSHOOKEX () functions are installed, the prototype of its function is as follows:
Setwindowshookex (
Int IDHOOK;
HookProc LPFN;
Hinstance hmod;
DWORD DWTHREADID;
); Parameter Description:
IDHOOK is the type of "hook", "hook" type has 13 types, as shown in the following table:
"Hook" type
Explanation
WH_CallWndProc
The system sends the message to the "hook" before the specified window.
WH_CallWndProcret
"Hook" that has been processed in the window
WH_CBT
"Hook" based on computer training
WH_Debug
Error "hook"
Wh_ForeGroundIdle
Front desk idle window "hook"
WH_GetMessage
Receive message delivery "hook"
Wh_journalplayback
Play back the input message recorded through the WH_JournalRecord "hook"
WH_JournalRecord
Enter a message record "hook"
WH_Keyboard
Keyboard message "hook"
WH_MOUSE
Mouse message "hook"
WH_MSGFILTER
Dialog, message box, menu, or scroll bar input message "hook"
WH_SHELL
Shell "hook"
WH_SYSMSGFILTER
System message "hook"
LPFN points to the pointer to the "hook" process.
The HMOD "Hook" process The handle of the module is located.
DWTHREADID The logo of the "hook" related thread.
Usually we all make the "hook" into a dynamic link library, which is that each process within the system.
access. But can also be called directly in the system, my suggestion is still using a dynamic library. If you use a dynamic library, then
What is the third parameter in setWindowsHooKex () is the handle of the dynamic link library module; for a single supply
The "hook" accessed accessed, you can put its "hook" process in the same thread of the "hook" process, this
The third parameter in SetWindowsHookex () is the Hinstance of the thread. There are two ways to install "hook"
: 1. You can make him a dynamic connection library file, compile together with the program. 2. You can go anywhere in the program
Toned. The second method is too trouble, I don't recommend it, I will not introduce it here. Compared with the first
easier. Its "hook" process is completed in the dynamic link library. Setwindowshookex () function is one
Install the function, such as an event that is monitored by some type of "hook", the system will call the corresponding type.
The "hook" process at the "hook" chain, each "hook" process of the "hook" chain should consider whether
The piece is passed to the next "hook" process. If you want to pass, you want to call the callnesthooKex () function. This
When the function is successful, return the return value of the next "hook" process in the "hook" link, the type of return value depends on "
Type of hooks. The prototype of this function is as follows:
LResult CallNexthookex (
HHOOK HHK;
Int ncode;
WPARAM WPARAM;
LParam Lparam;
); Where HHK is the current "hook" handle, it is returned by the setWindowsHooKex () function. Ncode is passed "
The event code for the hook process .WParam and LPARAM are transmitted to the "hook" process, respectively, and the specific meaning is related to the "hook" type.
Release "hook"
Release "hook" is relatively simple, he has only one parameter. When it is not required to "hook", it should be released in time.
put. He is to call UNHOOKWINDOWSHOKEX () functions, and the function prototype is as follows:
UnHookWindowsHookex (
HHOOK HHK;
);
The function successfully returns true, otherwise returns false.
If I don't understand this, please see some of the typical "hook" code and instructions given below.
LResult WinAPI CallWndProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
IF (ncode <0)
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
Switch (ncode)
{
Case HC_Action:
// "Hook" program to handle what code
Break;
DEFAULT:
Break;
}
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
}
This is the code of the WH_CallWndProc "hook", this "hook" allows the program to monitor by the function sendMessag
E Send a message to the window process. The system calls the wh_callwndproc before sending the message to the destination window
"process.
Lresult WinAPI CallWndProc (int Ncode, WPARAM, WPARAM, LPARAM LPARAM)
{
IF (Ncode <0) Return CallnexthooKex (Null, Ncode, WPARAM, LPARAM);
Switch (ncode)
{
Case HC_Action:
Switch (WPARAM)
{
Case PM_Remove:
// An application calls the GetMessage function or the // peekmessage function with the PM_Remove parameter,
Remove one message from the message queue.
Break;
Case PM_NOREMOVE:
// An application calls the peekMessage function as a parameter in PM_NOREMOVE
Break;
DEFAULT:
Break;
}
Break;
DEFAULT:
Break;
}
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
}
This is a function that calls wh_getMessage. This function allows the application monitoring function GetMessage and Peek
Message returned to the message. The application can use hook wh_getMessage to monitor the input of the mouse and keyboard and
Other systems are sent to messages in the message queue.
LResult Callback CBTProc (Int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
IF (Ncode <0) Return CallnexthooKex (Null, Ncode, WPARAM, LPARAM);
Switch (ncode)
{
Case hcbt_activate:
// The system will activate a window
Break;
Case HCBT_CLICKSKIPPED:
/ / The system removes a mouse message from the system message queue
Break;
Case HCBT_CREATEWND:
// The system will create a window
Break;
Case HCBT_DESTROYWND:
// The system will close a window
Break;
Case HCBT_KEYSKIPPED:
/ / The system removes a keyboard message from the system message queue
Break;
Case HCBT_MINMAX:
/ / The system will maximize or minimize one window
Break;
Case HCBT_MOVESIZE:
// The system will move a window or change the size of a window.
Break; Case HCBT_QS:
/ / The system retrieves WM_QUEESYNC messages in the system message queue
Break;
Case HCBT_SETFOCUS:
// System Setup Keyboard Enter Window
Break;
Case HCBT_SYSCOMMAND:
// will execute a system command
Break;
DEFAULT:
/ / Can add additional code
Break;
}
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
}
Each "hook" type has its corresponding function, the parameters of these functions are the same, interested friends
You can make a detailed description of the business of cherry.
Below I give a complete "hook" installation and delete process code.
#include "stdafx.h"
#include "hook.h"
Hinstance hinstance;
Hhook hhkkeyboard;
Bool Apientry Dllmain (Handle Hmodule, DWORD UL_REASON_FOR_CALL, LPVOID LPRESERVED)
{
Switch (ul_reason_for_call)
{
Case DLL_Process_attach:
Case DLL_THREAD_ATTACH:
Case DLL_THREAD_DETACH:
Case DLL_PROCESS_DETACH:
Break;
}
Hinstance = (hinstance) hmodule;
Return True;
}
LResult KeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
MessageBeep (-1);
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
}
Hook_api bool enableKeyboardCapture ()
{
IF (! (hh_keyboard = setwindowshookex (wh_keyboard, (hookproc) KeyboardProc, hinstance, 0))))
Return False;
Return True;
}
Hook_api bool disableKeyboardCapture ()
{
Return UnhookWindowsHookex (HHKKEYBOARD);
} Note: This is a dynamic link library file.
When you want to call "hook" in the program, you can have an enableKeyboardCapture () function, but you press
When the key is time, I will send a sound.