Hook is a platform for message processing in Windows. Applications can set sub-processes on the platform to monitor messages in a specified window, the monitored window can be created by other processes. When a message arrives, process it before the target window processing function. The hook mechanism allows applications to intercept and process window messages or specific events.
Hook detailed introduction, in Microsoft's MSDN, http://www.microsoft.com/china/community/program/originalarticles/techdoc/hook.mspx
The following shows how to apply the Hook in C:
Effect:
When you enter B in TextBox, TextBox always displays
Implementation process:
1. Create a C # Windows Application
2. In Form1, add the following variables:
Internal enum HookType // enumeration, hook type
{
// MsgFilter =-1,
// JournalRecord = 0,
// JournalPlayback = 1,
Keyboard = 2,
// GetMessage = 3,
// CallWndProc = 4,
// CBT = 5,
// SysMsgFilter = 6,
// Mouse = 7,
// Hardware = 8,
// Debug = 9,
// Shell = 10,
// ForegroundIdle = 11,
// CallWndProcRet = 12,
// KeyboardLL = 13,
// MouseLL = 14,
};
IntPtr _ nextHookPtr; // record the Hook number
3. Introduce required APIs in Form1
[DllImport ("kernel32.dll")]
Static extern int GetCurrentThreadId (); // obtain the ID of the current thread
[DllImport ("User32.dll")]
Internal extern static void UnhookWindowsHookEx (IntPtr handle); // cancel the Hook API
[DllImport ("User32.dll")]
Internal extern static IntPtr SetWindowsHookEx (int idHook, [financialas (UnmanagedType. FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); // sets the Hook API
[DllImport ("User32.dll")]
Internal extern static IntPtr CallNextHookEx (IntPtr handle, int code, IntPtr wparam, IntPtr lparam); // obtain the next Hook API
4. Declare an implementation delegate
Internal delegate IntPtr HookProc (int code, IntPtr wparam, IntPtr lparam );
5. add your own Hook processing process
IntPtr MyHookProc (int code, IntPtr wparam, IntPtr lparam)
{
If (code <0) return CallNextHookEx (_ nextHookPtr, code, wparam, lparam); // return, let the subsequent program process the message
If (wparam. ToInt32 () = 98 | wparam. ToInt32 () = 66) // if the user inputs B
{
This. textBox1.Text = "";
Return (IntPtr) 1; // The message is returned directly, and the processing is complete.
}
Else
{
Return IntPtr. Zero; // return, let the subsequent program process the message
}
}
6. Add functions that are added to and canceled from the Hook chain
Public void SetHook ()
{
If (_ nextHookPtr! = IntPtr. Zero) // already checked
Return;
HookProc myhookProc = new HookProc (MyHookProc); // declare a delegate object of your own Hook implementation function
_ NextHookPtr = SetWindowsHookEx (int) HookType. Keyboard, myhookProc, IntPtr. Zero, GetCurrentThreadId (); // Add it to the Hook chain
}
Public void UnHook ()
{
If (_ nextHookPtr! = IntPtr. Zero)
{
UnhookWindowsHookEx (_ nextHookPtr); // cancel from the Hook chain
_ NextHookPtr = IntPtr. Zero;
}
}
7. Add SetHook () to the Load event of Form1, and add UnHook () to the closing event of Form1 ()
Private void Form1_Load (object sender, System. EventArgs e)
{
SetHook ();
}
Private void Form1_Closing (object sender, System. ComponentModel. CancelEventArgs e)
{
UnHook ();
}
8. Run
Enter B and the textbox shows!