private WindowHookProc _hookProc; // Keep a reference to prevent garbage collection. public WindowHook(WindowHookType type) { _hookProc = new WindowHookProc(HookProc); _handle = SetWindowsHookEx( (int)type, _hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId() ); }
public static IntPtr SetWindowsHook(HookType hookType, WindowHookProc lpfnHookProc) { try { using (var currentProcess = Process.GetCurrentProcess()) { using (var currentModule = currentProcess.MainModule) { IntPtr hModule = Win32HookAPI.GetModuleHandle(currentModule.ModuleName); if (hModule != IntPtr.Zero) { return(Win32HookAPI.SetWindowsHookEx(hookType, lpfnHookProc, hModule, 0)); } } } } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } return(IntPtr.Zero); }
private static extern IntPtr SetWindowsHookEx(int hookId, WindowHookProc callback, IntPtr hInstance, int nativeThreadId);
internal static extern IntPtr SetWindowsHookEx(HookType hookType, WindowHookProc pfn, IntPtr hMod, uint dwThreadId);
public AsyncWindowsHook(HookType hookType, WindowHookProc pfnClientHookProc) : base(hookType, pfnClientHookProc) { }
// passed-in routine used for call-back public WindowsHook(HookType hookType, WindowHookProc pfnClientHookProc) { this.hookType = hookType; this.pfnClientHookProc = pfnClientHookProc; SetWindowsHook(); }