Hook Programming in MFC

Source: Internet
Author: User

Hook,n. Hooks, hooks, usually called hooks.

In a computer, a platform for Windows message processing, where an application can set a subroutine to monitor a message for a specified form, and the form being monitored can be created by another process. When the message arrives, it is processed before the target form processing function. The hook mechanism agrees that the application intercepts the processing of window messages or specific events.
   A hook is actually a program segment that processes messages, and it is put into the system by a system call. Whenever a particular message is issued, the hook program captures the message before the destination form is reached, i.e. the hook function gets control first. The hook function is then able to process (change) the message, to continue to pass the message without processing, and to force the end of the message delivery.

Hook principle
   Each hook has a list of pointers associated with it, called a chain of hooks, which is maintained by the system. The pointer to this list points to the specified, application-defined, callback function called by the hook, which is the various processing routines for the hooks. When the message associated with the specified hook type occurs, the system passes the message to the hook thread. Some hooks can only monitor messages, or change messages, or stop the progress of messages, to prevent these messages from being passed to the next hook or destination form. Recently installed hooks are placed at the beginning of the chain, and the first installed hooks are placed at the end, that is, after the addition of the first gain control.
   Windows does not require that the order of the hooks be unloaded in the reverse order of installation. Whenever a hook is unloaded, Windows frees up its occupied memory and updates the entire hook list. Suppose the program installs a hook, but before the hook has not been uninstalled, then the system will take the initiative to do the uninstall hook operation.
A hook thread is an application-defined callback (CALLBACK function) that cannot be defined as a member function of a class, but can only be defined as a normal C function. Used to monitor a system or a specific type of event that can be associated with a particular thread, or it can be an event for all threads in the system.
  System hooks and Thread hooks
   The last parameter of the SetWindowsHookEx () function determines whether the hook is a system hook or a thread hook.
   The line Cheng is used to monitor event messages for a specified thread. A line Cheng is typically within a thread that is derived from the current thread or when it is active.
   The system hook monitors the event messages for all threads in the system. Because the system hook affects all applications in the system, the hook function must be placed in a separate dynamic-link library (DLL). The system itself proactively maps the DLL that includes the "Hook callback function" to the address space of all processes affected by the hook function, injecting the DLL into those processes.

Msdn

hhook SetWindowsHookEx (  intidhook,        //Hook type  HOOKPROCLPFN  ,     //Hook procedure  hinstancehmod,    //handle to Application instance  DWORDdwthreadid   //thread identifier);
The Fourth of these:
 
   
dwThreadID
Specifies the identifier of the
thread with which the hook procedure
are to be associated.
If This parameter is zero, the hook procedure are associated with all existing threads running
In the
same desktop as the calling thread.

For example

DLL to set the hook

void Sethook (HWND hwnd)
{
g_hwnd=hwnd;//is a clever way to pass a handle to a process that calls it.
G_hmouse=setwindowshookex (Wh_mouse,mouseproc,getmodulehandle ("Hook"),0);//The fourth parameter is 0, which indicates that this hook is the whole process of the current desktop. The system itself proactively maps the DLL that includes the "Hook callback function" to the address space of all processes affected by the hook function, injecting the DLL into those processes.
G_hkeyboard=setwindowshookex (Wh_keyboard,keyboardproc,getmodulehandle ("Hook"),0);
}

Application of hooks:
Observation mode
Most often, Setwindowhook, like Windows provides, is typically prepared for such applications. And this is the most common way to use it.
This pattern is characterized by the sending of a notification message when something happens. Observers can only view the information in the process, handle their business based on what they care about, but cannot change the original process.
such as global hooks, the use of mouse messages, keyboard message monitoring and other applications. PowerWord screen to take the word function is a typical application (detailed technology can refer to this kind of article).
Injection mode
The biggest difference between this pattern and the observation pattern is that the injected code is intended to extend the functionality of the original code to the business. Plug-in mode is a typical case of this type of pattern.
Regardless of the thin core plug-in system (such as Eclipse) or the Fat core plug-in system (such as Delphi, Visual Studio and other IDE environments), its external plug-in interface is to extend the functionality of its own system.
The typical feature of such an extended application is that the new extension code and the original code will coordinate with the same business.
Replacement mode
Assume that the needle corresponding to the purpose of different, can be called repair mode or crack mode. The former is to change the system bug, the latter is to crack the original system limitations.
A lot of hackers use this mode to replace the export table in the DLL that accessed the encryption lock with their own function, so that the control code for the software is skipped. The difficulty with this type of application is to find out the function's parameters.
The feature of this pattern is that the original code is replaced by the new code.
The first three are basic patterns, and there are a lot of patterns associated with practical applications.
Centralization mode
The emergence of such patterns is mostly to deal with certain kinds of things uniformly in all systems. It is not characterized by the way it is injected, but by the mode of processing.
This mode is mostly applied to certain services, such as keyboard service, mouse service, printer service and so on. Restrict or coordinate access to services by unifying access to such services.
For example, the keyboard lock function is implemented to temporarily turn off all applications of the keyboard.
The characteristics of such models are mainly related to the characteristic services.
Repair mode
Substitution pattern, which is emphasized here is the purpose of the application is to repair or extend the functionality of the original system.
Crack mode
One of the replacement patterns, which emphasizes that the purpose of the application is to skip part of the code of the original system. such as cryptographic detection code, network detection code, and so on.
Plug-in mode
Injection mode, which directly relies on the hook mechanism in the system to expand the business function.
Sharing mode
In such applications, it is often to obtain data from each other. Must I want to get the value of all the strings in each other's system. The ability to export all strings by replacing each other's memory manager.
This application is more special. It is only characterized in that the purpose is to achieve data sharing between systems.
In fact, it may be an observation pattern or a replacement pattern.

Hook Programming in MFC

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.