/// <summary> /// Attach the given handler to intercept the given message types /// </summary> /// <param name="messageType"></param> /// <param name="handler"></param> /// <returns></returns> public static IntPtr SetHook(HookID messageType, HookProcess handler) { using (Process currentProcess = Process.GetCurrentProcess()) { using (ProcessModule currentModule = currentProcess.MainModule) { var hook = SetWindowsHookEx(Convert.ToInt32(messageType), handler, GetModuleHandle(currentModule.ModuleName), 0); lock (CurrentHooksLock) { CurrentlySetHooks.Add(hook, handler); } return(hook); } } }
private static extern IntPtr SetWindowsHookEx([In] HOOK_TYPE hookType, [In] HookProcess hookProcess, [In] IntPtr handle, [In] UInt32 threadId);
public static IntPtr Hook(HookProcess process) { UInt32 threadId = GetCurrentThreadId(); return((threadId == 0) ? IntPtr.Zero : SetWindowsHookEx(HOOK_TYPE.WH_CALLWNDPROCRET, process, IntPtr.Zero, threadId)); }
public static extern IntPtr SetWindowsHookEx(int idHook, HookProcess lpfn, IntPtr hMod, uint dwThreadId);
public static extern int SetWindowsHookEx(int idHook, HookProcess lpfn, IntPtr hInstance, int threadId);