Select BLOG keyword from XDEV [Reference] C # hook this thread intercept message interception http://blog.9cbs.net/yiruoyun/archive/2004/10/17/140219.aspx hooks actually call API: 1 , Install the hook: SETWINDOWSHOKEX function protest: hHOOK SETWINDOWSHOKEX (int IDHOK, // hook type, hookproc lpfn, // hook function address instance of the instance hmod, // hook, DWord DwthreadID // Monkey Monethey Thread Number HMOD: For the line program hook, parameter biol; for the system hook: Parameter is the handle of the hook DLL DWTHREADID: For global hook, this parameter is NULL. The hook type uses wh_callwndproc = 4 (send to the window message. Triggered by sendMessage): Success: Return to SetWindowsHookex Returns the installed hook handle; fail: null; 2, callback, you have to intercept the message here: LResult WinApi myhookProc (INT ncode, / / Specify whether it needs to handle the message wparam wparam, // contains additional messages containing the message lParam lparam // contains additional messages for the message) 3, call the next hook LRESULT CallNexthookex (hHOOK hHK, // is you Handle of your own hook function. Use this handle to traverse the hook chain int ncode, // simply pass the incoming parameters to CallNexthookex can wparam wparam, // Simply pass the incoming parameters to CallNexthookex LParam lparam /// Simply pass the incoming parameters to CallNexthookex); 4, remember to uninstall the hook after you have finished using it! Bool UnhookWindowsHookex (hHOOK HHK / / Hook handle to uninstall.
Pack the above API with C # pack, you can use it directly! Give a threaded example (two forms run in the same thread): use system.Runtime.InterOpServices; public class form1: system.windows.forms.form {... // Define delegation (hook function, callback) public delegate int HookProc (int code, IntPtr wparam, ref CWPSTRUCT cwp); // mounting hook function [DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowsHookEx (int type , HookProc hook, IntPtr instance, int threadID); // call to a next hook function [DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern int CallNextHookEx (IntPtr hookHandle, int code, IntPtr wparam, ref CWPSTRUCT cwp); // unloading hook [DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern bool UnhookWindowsHookEx (IntPtr hookHandle); // Get form thread ID DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId (IntPtr hwnd, int ID); private HookProc hookProc; private IntPtr hookHandle = IntPtr.Zero; public Form1 () {.... // articulated hook approach this. HookProc = New hookpr OC (MyHOKPROC);} // Start Intercept Private Bool StartHook () {Form2 F = New Form2 (); f.show (); // Plus this // Install the hook, intercept the system to Form2 message this.hookhandle = SetWindowsHookEx (4, hookProc, IntPtr.Zero, GetWindowThreadProcessId (f.Handle, 0)); return (this.hookHandle = 0!);} // intercepting private bool StopHook () {return UnhookWindowsHookEx (this.hookHandle); } // hook handler,
Intercept the message here and do the Private Int MyHookProc (int Code, INTPTR WPARAM, REF CWPSTRUCT CWP) {switch (code) {case 0: switch (cwp.message) {case 0x0000f: // wm_paint, blocking WM_PAINT message // DO something break;} break;} return CallNextHookEx (hookHandle, code, wparam, ref cwp);} [StructLayout (LayoutKind.Sequential)] public struct CWPSTRUCT {public IntPtr lparam; public IntPtr wparam; public int message; public IntPtr hwnd;} } public class form2: system.windows.forms.form {....} hook (hook) is a platform for the Windows message processing mechanism, and the application can set a subtertorthy to monitor some of the message of 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/community/program/originalarticles/techdoc/hook.mspx
Here is I am in c # to apply hook:
Effect:
When the user enters B in TextBox, TextBox always displays a
Implementation process:
1. Newly built a C # WindowsApplication
2, 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. Introduce the necessary API in Form1
[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 API
4. Declare an implementation of the commission of INTERNAL DELEGATE INTPTR HOOKPROC (int Code, INTPTR WPARAM, INTPTR LPARAM);
5, add your own HOOK processing
INTPTR MYHOKPROC (int code, intptr wparam, int code lparam)
{
IF (Code <0) Return CallNexthookex (_nexThookPtr, Code, WPARAM, LPARAM); // Returns, let the subsequent 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!