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