WndProcRetHook.cs
author sl
Sat, 14 Jun 2014 12:51:25 +0200
changeset 0 f6eca6facd07
permissions -rw-r--r--
First contrib.
     1 //=============================================================================
     2 // COPYRIGHT: Prosoft-Lanz
     3 //=============================================================================
     4 //
     5 // $Workfile: WndProcRetHook.cs $
     6 //
     7 // PROJECT : CodeProject Components
     8 // VERSION : 1.00
     9 // CREATION : 19.02.2003
    10 // AUTHOR : JCL
    11 //
    12 // DETAILS : This class implement the WH_CALLWNDPROCRET Windows hook mechanism.
    13 //           From MSDN, Dino Esposito.
    14 //
    15 //           WindowCreate, WindowDestroye and WindowActivate user events.
    16 //
    17 //-----------------------------------------------------------------------------
    18 using System;
    19 using System.Runtime.InteropServices;
    20 using System.Diagnostics;
    21 
    22 namespace CodeProject.Win32API.Hook
    23 {
    24 	///////////////////////////////////////////////////////////////////////
    25 	#region Enum WndMessage
    26 
    27 	/// <summary>
    28 	/// windows message.
    29 	/// </summary>
    30 	public enum WndMessage : int
    31 	{
    32 		/// Sent to the dialog procedure immediately before the dialog is displayed.
    33 		WM_INITDIALOG = 0x0110,
    34 		/// Sent to the dialog procedure immediately before the dialog is displayed.
    35 		WM_UNKNOWINIT = 0x0127
    36 	}
    37 	#endregion
    38 
    39 	///////////////////////////////////////////////////////////////////////
    40 	#region Class WndProcRetEventArgs
    41 
    42 	/// Class used for WH_CALLWNDPROCRET hook event arguments.
    43 	public class WndProcRetEventArgs : EventArgs
    44 	{
    45 		/// wParam parameter.
    46 		public IntPtr wParam;
    47 		/// lParam parameter.
    48 		public IntPtr lParam;
    49 		/// CWPRETSTRUCT structure.
    50 		public CwPRetStruct cw;
    51 
    52 		internal WndProcRetEventArgs(IntPtr wParam, IntPtr lParam)
    53 		{
    54 			this.wParam = wParam;
    55 			this.lParam = lParam;
    56 			cw = new CwPRetStruct();
    57 			Marshal.PtrToStructure(lParam, cw);
    58 		}
    59 	}
    60 
    61 	/// <summary>
    62 	/// CWPRETSTRUCT structure.
    63 	/// </summary>
    64 	[StructLayout(LayoutKind.Sequential)]
    65 	public class CwPRetStruct
    66 	{
    67 		/// Return value.
    68 		public int lResult;
    69 		/// lParam parameter.
    70 		public int lParam;
    71 		/// wParam parameter.
    72 		public int wParam;
    73 		/// Specifies the message.
    74 		public WndMessage message;
    75 		/// Handle to the window that processed the message.
    76 		public IntPtr hwnd;
    77 	}
    78 
    79 	#endregion
    80 
    81 	///////////////////////////////////////////////////////////////////////
    82 	#region Class WndProcRetHook
    83 	
    84 	/// <summary>
    85 	/// Class to expose the windows WH_CALLWNDPROCRET hook mechanism.
    86 	/// </summary>
    87 	public class WndProcRetHook : WindowsHook
    88 	{
    89 		/// <summary>
    90 		/// WH_CALLWNDPROCRET hook delegate method.
    91 		/// </summary>
    92 		public delegate void WndProcEventHandler(object sender, WndProcRetEventArgs e);
    93 
    94 		private IntPtr hWndHooked;
    95 
    96 		/// <summary>
    97 		/// Window procedure event.
    98 		/// </summary>
    99 		public event WndProcEventHandler WndProcRet;
   100 
   101 		/// <summary>
   102 		/// Construct a WH_CALLWNDPROCRET hook.
   103 		/// </summary>
   104 		/// <param name="hWndHooked">
   105 		/// Handle of the window to be hooked. IntPtr.Zero to hook all window.
   106 		/// </param>
   107 		public WndProcRetHook(IntPtr hWndHooked) : base(HookType.WH_CALLWNDPROCRET)
   108 		{
   109 			this.hWndHooked = hWndHooked;
   110 			this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
   111 		}
   112 		/// <summary>
   113 		/// Construct a WH_CALLWNDPROCRET hook giving a hook filter delegate method.
   114 		/// </summary>
   115 		/// <param name="hWndHooked">
   116 		/// Handle of the window to be hooked. IntPtr.Zero to hook all window.
   117 		/// </param>
   118 		/// <param name="func">Hook filter event.</param>
   119 		public WndProcRetHook(IntPtr hWndHooked, HookProc func) : base(HookType.WH_CALLWNDPROCRET, func)
   120 		{
   121 			this.hWndHooked = hWndHooked;
   122 			this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
   123 		}
   124 
   125 		// handles the hook event
   126 		private void WndProcRetHookInvoked(object sender, HookEventArgs e)
   127 		{
   128 			WndProcRetEventArgs wpe = new WndProcRetEventArgs(e.wParam, e.lParam);
   129 			if ((hWndHooked == IntPtr.Zero || wpe.cw.hwnd == hWndHooked) && WndProcRet != null)
   130 				WndProcRet(this, wpe);
   131 			return;
   132 		}
   133 	}
   134 	#endregion
   135 }