Adding user given name to EAR events and enabling User Events.
1 //=============================================================================
2 // COPYRIGHT: Prosoft-Lanz
3 //=============================================================================
5 // $Workfile: WindowsHook.cs $
7 // PROJECT : CodeProject Components
9 // CREATION : 19.02.2003
12 // DETAILS : This class implement the Windows hook mechanism.
13 // From MSDN, Dino Esposito.
15 //-----------------------------------------------------------------------------
17 using System.Runtime.InteropServices;
19 namespace CodeProject.Win32API.Hook
21 ///////////////////////////////////////////////////////////////////////
22 #region Class HookEventArgs
24 /// Class used for hook event arguments.
25 public class HookEventArgs : EventArgs
27 /// Event code parameter.
34 internal HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
44 ///////////////////////////////////////////////////////////////////////
48 public enum HookType : int
53 WH_JOURNALPLAYBACK = 1,
73 WH_FOREGROUNDIDLE = 11,
75 WH_CALLWNDPROCRET = 12,
83 ///////////////////////////////////////////////////////////////////////
84 #region Class WindowsHook
87 /// Class to expose the windows hook mechanism.
89 public class WindowsHook
92 /// Hook delegate method.
94 public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
96 // internal properties
97 internal IntPtr hHook = IntPtr.Zero;
98 internal HookProc filterFunc = null;
99 internal HookType hookType;
102 /// Hook delegate method.
104 public delegate void HookEventHandler(object sender, HookEventArgs e);
107 /// Hook invoke event.
109 public event HookEventHandler HookInvoke;
111 internal void OnHookInvoke(HookEventArgs e)
113 if (HookInvoke != null)
118 /// Construct a HookType hook.
120 /// <param name="hook">Hook type.</param>
121 public WindowsHook(HookType hook)
124 filterFunc = new HookProc(this.CoreHookProc);
127 /// Construct a HookType hook giving a hook filter delegate method.
129 /// <param name="hook">Hook type</param>
130 /// <param name="func">Hook filter event.</param>
131 public WindowsHook(HookType hook, HookProc func)
137 // default hook filter function
138 internal int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
141 return CallNextHookEx(hHook, code, wParam, lParam);
143 // let clients determine what to do
144 HookEventArgs e = new HookEventArgs(code, wParam, lParam);
147 // yield to the next hook in the chain
148 return CallNextHookEx(hHook, code, wParam, lParam);
152 /// Install the hook.
154 public void Install()
156 hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
161 /// Uninstall the hook.
163 public void Uninstall()
165 if (hHook != IntPtr.Zero)
167 UnhookWindowsHookEx(hHook);
172 #region Win32 Imports
174 [DllImport("user32.dll")]
175 internal static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
177 [DllImport("user32.dll")]
178 internal static extern int UnhookWindowsHookEx(IntPtr hhook);
180 [DllImport("user32.dll")]
181 internal static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);