diff -r 000000000000 -r f6eca6facd07 WndProcRetHook.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WndProcRetHook.cs Sat Jun 14 12:51:25 2014 +0200 @@ -0,0 +1,135 @@ +//============================================================================= +// COPYRIGHT: Prosoft-Lanz +//============================================================================= +// +// $Workfile: WndProcRetHook.cs $ +// +// PROJECT : CodeProject Components +// VERSION : 1.00 +// CREATION : 19.02.2003 +// AUTHOR : JCL +// +// DETAILS : This class implement the WH_CALLWNDPROCRET Windows hook mechanism. +// From MSDN, Dino Esposito. +// +// WindowCreate, WindowDestroye and WindowActivate user events. +// +//----------------------------------------------------------------------------- +using System; +using System.Runtime.InteropServices; +using System.Diagnostics; + +namespace CodeProject.Win32API.Hook +{ + /////////////////////////////////////////////////////////////////////// + #region Enum WndMessage + + /// + /// windows message. + /// + public enum WndMessage : int + { + /// Sent to the dialog procedure immediately before the dialog is displayed. + WM_INITDIALOG = 0x0110, + /// Sent to the dialog procedure immediately before the dialog is displayed. + WM_UNKNOWINIT = 0x0127 + } + #endregion + + /////////////////////////////////////////////////////////////////////// + #region Class WndProcRetEventArgs + + /// Class used for WH_CALLWNDPROCRET hook event arguments. + public class WndProcRetEventArgs : EventArgs + { + /// wParam parameter. + public IntPtr wParam; + /// lParam parameter. + public IntPtr lParam; + /// CWPRETSTRUCT structure. + public CwPRetStruct cw; + + internal WndProcRetEventArgs(IntPtr wParam, IntPtr lParam) + { + this.wParam = wParam; + this.lParam = lParam; + cw = new CwPRetStruct(); + Marshal.PtrToStructure(lParam, cw); + } + } + + /// + /// CWPRETSTRUCT structure. + /// + [StructLayout(LayoutKind.Sequential)] + public class CwPRetStruct + { + /// Return value. + public int lResult; + /// lParam parameter. + public int lParam; + /// wParam parameter. + public int wParam; + /// Specifies the message. + public WndMessage message; + /// Handle to the window that processed the message. + public IntPtr hwnd; + } + + #endregion + + /////////////////////////////////////////////////////////////////////// + #region Class WndProcRetHook + + /// + /// Class to expose the windows WH_CALLWNDPROCRET hook mechanism. + /// + public class WndProcRetHook : WindowsHook + { + /// + /// WH_CALLWNDPROCRET hook delegate method. + /// + public delegate void WndProcEventHandler(object sender, WndProcRetEventArgs e); + + private IntPtr hWndHooked; + + /// + /// Window procedure event. + /// + public event WndProcEventHandler WndProcRet; + + /// + /// Construct a WH_CALLWNDPROCRET hook. + /// + /// + /// Handle of the window to be hooked. IntPtr.Zero to hook all window. + /// + public WndProcRetHook(IntPtr hWndHooked) : base(HookType.WH_CALLWNDPROCRET) + { + this.hWndHooked = hWndHooked; + this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked); + } + /// + /// Construct a WH_CALLWNDPROCRET hook giving a hook filter delegate method. + /// + /// + /// Handle of the window to be hooked. IntPtr.Zero to hook all window. + /// + /// Hook filter event. + public WndProcRetHook(IntPtr hWndHooked, HookProc func) : base(HookType.WH_CALLWNDPROCRET, func) + { + this.hWndHooked = hWndHooked; + this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked); + } + + // handles the hook event + private void WndProcRetHookInvoked(object sender, HookEventArgs e) + { + WndProcRetEventArgs wpe = new WndProcRetEventArgs(e.wParam, e.lParam); + if ((hWndHooked == IntPtr.Zero || wpe.cw.hwnd == hWndHooked) && WndProcRet != null) + WndProcRet(this, wpe); + return; + } + } + #endregion +}