Hook, is a platform for the Windows message handling mechanism, which can be set up on top to monitor some of the messages for the specified window, and the monitored window can be created by other processes. When the message arrives, handle it before the target window handles the function. The hook mechanism allows applications to intercept the Window message or a specific event.
Detailed introduction to Hook, in Microsoft's MSDN, http://www.microsoft.com/china/techdoc/hook.mspx Below is I am in c # to apply hook: implementation: When When the user enters B in TextBox, TextBox always displays a implementation process: 1. New C #'s WindowsApplication2, in Form1, add some variables below:
Internal enum hooktype // enumeration, hook type
{
// msgfilter = -1,
// jensionrecord = 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 _NEXTHOKPTR; // Record the hook number
3. Introduced the necessary API [DLLIMPORT ("kernel32.dll")] static extern int getcurrentthreadId (); // get the current thread number API
[DLLIMPORT ("User32.dll")]]]]]
INTERNAL EXTERN Static Void UnhookWindowsHookex (INTPTR HANDLE); // Cancel Hook API
[DLLIMPORT ("User32.dll")]]]]]
INTERNAL EXTERN Static INTPTR SETWINDOWSHOKEX (Int IDHOK, [Marshalas (UnmanagedType.FunctionPtr)] HookProc LPFN, INTPTR HINSTANCE, INT THREADID; // Setting HOOK API
[DLLIMPORT ("User32.dll")]]]]]
internal extern static IntPtr CallNextHookEx (IntPtr handle, int code, IntPtr wparam, IntPtr lparam); // get the next Hook of API4, declare a delegate to achieve internal delegate IntPtr HookProc (int code, IntPtr wparam, IntPtr lparam); 5, Add your own HOOK process
INTPTR MYHOKPROC (int code, intptr wparam, int code lparam)
{
IF (Code <0) Return CallNexthookex (_nexThookPtr, Code, WPARAM, LPARAM); // Returns, let the rear program processing the message if (wParam.Toint32 () == 98 || WPARAM.TOINT32 () == 66) // If the user enters B
{
THIS.TEXTBOX1.TEXT = "a";
Return (INTPTR) 1; / / directly returned, the message ended
}
Else
{
Return INTPTR.ZERO; / / Return, let the rear program process the message
}
}
6, add a function that is added to the HOOK chain and cancel from the hook chain
Public void setook ()
{
IF (_nexthookptr! = INTPTR.ZERO) // has already been hooked
Return;
HookProc MyHookProc = New HookProc (myhookProc); // Declare a delegation object of your own Hook implementation function
_nexthookptr = setWindowshookex ((int) hooktype.keyboard, myhookproc, intptr.zero, getcurrentthreadid ()); // Add to HOOK chain
}
Public void unhook ()
{
IF (_NexThookPtr! = INTPTR.ZERO)
{
UnHookWindowsHookex (_nexthookptr); // Cancel from the hook chain
_nexthookptr = intptr.zero;
}
}
7. Add setHook () in Form1's LOAD event, add unHook () in the forming event of Form1
Private Void Form1_Load (Object Sender, System.EventArgs E)
{
Sthook ();
}
Private void form1_closing (Object Sender, System.comPonentmodel.canceleventargs E)
{
UnHook ();
}
8, run the input B, discover the display in TextBox is A! Summary: This is a very simple Hook application, when we are doing a Windows program, these things may be used. I am trying to make a relatively universal class to make a package so that better use later. Strive for ing ...