private void Install(InstalledHook toInstall) { //the corresponding windows APIs work only with the obsolete method! #pragma warning disable 0618 int threadId = AppDomain.GetCurrentThreadId(); #pragma warning restore 0618 if ((toInstall & InstalledHook.CallWndProc) != 0 && (this.installedHook & InstalledHook.CallWndProc) == 0) { this.callWndProcHookAddress = SetWindowsHookEx(HookType.WH_CALLWNDPROC, this.callWndProcDelegate, IntPtr.Zero, threadId); this.installedHook |= InstalledHook.CallWndProc; } if ((toInstall & InstalledHook.GetMessage) != 0 && (this.installedHook & InstalledHook.GetMessage) == 0) { this.getMessageHookAddress = SetWindowsHookEx(HookType.WH_GETMESSAGE, this.getMessageDelegate, IntPtr.Zero, threadId); this.installedHook |= InstalledHook.GetMessage; } if ((toInstall & InstalledHook.SystemMessage) != 0 && (this.installedHook & InstalledHook.SystemMessage) == 0) { this.systemMessageHookAddress = SetWindowsHookEx(HookType.WH_MSGFILTER, this.systemMessageDelegate, IntPtr.Zero, threadId); this.installedHook |= InstalledHook.SystemMessage; } }
private void Uninstall(InstalledHook toUninstall) { if ((toUninstall & InstalledHook.CallWndProc) != 0 && (this.installedHook & InstalledHook.CallWndProc) != 0) { UnhookWindowsHookEx(this.callWndProcHookAddress); this.callWndProcHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.CallWndProc; } if ((toUninstall & InstalledHook.GetMessage) != 0 && (this.installedHook & InstalledHook.GetMessage) != 0) { UnhookWindowsHookEx(this.getMessageHookAddress); this.getMessageHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.GetMessage; } if ((toUninstall & InstalledHook.SystemMessage) != 0 && (this.installedHook & InstalledHook.SystemMessage) != 0) { UnhookWindowsHookEx(this.systemMessageHookAddress); this.systemMessageHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.SystemMessage; } }
private RadMessageFilter() { this.getMessageDelegate = new RadMessageFilter.HookProc(this.GetMessageHookProc); this.callWndProcDelegate = new RadMessageFilter.HookProc(this.CallWndProcHookProc); this.systemMessageDelegate = new RadMessageFilter.HookProc(this.SystemMessageHookProc); this.installedHook = InstalledHook.None; this.listeners = new WeakReferenceList <IMessageListener>(true); this.enabled = true; }
/// <summary> /// Verifies that all the needed hooks are installed (depending on each listener's DesiredHook). /// </summary> private void EnsureInstalledHooks() { //we are not enabled, do nothing if (!this.enabled) { return; } InstalledHook desiredHook = this.GetDesiredHook(); if (this.installedHook == desiredHook) { return; } InstalledHook toInstall = InstalledHook.None; InstalledHook toUninstall = InstalledHook.None; if ((desiredHook & InstalledHook.CallWndProc) == InstalledHook.CallWndProc) { toInstall |= InstalledHook.CallWndProc; } else { toUninstall |= InstalledHook.CallWndProc; } if ((desiredHook & InstalledHook.GetMessage) == InstalledHook.GetMessage) { toInstall |= InstalledHook.GetMessage; } else { toUninstall |= InstalledHook.GetMessage; } if ((desiredHook & InstalledHook.SystemMessage) == InstalledHook.SystemMessage) { toInstall |= InstalledHook.SystemMessage; } else { toUninstall |= InstalledHook.SystemMessage; } this.Install(toInstall); this.Uninstall(toUninstall); }
private void Install(InstalledHook toInstall) { int currentThreadId = AppDomain.GetCurrentThreadId(); if ((toInstall & InstalledHook.CallWndProc) != InstalledHook.None && (this.installedHook & InstalledHook.CallWndProc) == InstalledHook.None) { this.callWndProcHookAddress = RadMessageFilter.SetWindowsHookEx(RadMessageFilter.HookType.WH_CALLWNDPROC, this.callWndProcDelegate, IntPtr.Zero, currentThreadId); this.installedHook |= InstalledHook.CallWndProc; } if ((toInstall & InstalledHook.GetMessage) != InstalledHook.None && (this.installedHook & InstalledHook.GetMessage) == InstalledHook.None) { this.getMessageHookAddress = RadMessageFilter.SetWindowsHookEx(RadMessageFilter.HookType.WH_GETMESSAGE, this.getMessageDelegate, IntPtr.Zero, currentThreadId); this.installedHook |= InstalledHook.GetMessage; } if ((toInstall & InstalledHook.SystemMessage) == InstalledHook.None || (this.installedHook & InstalledHook.SystemMessage) != InstalledHook.None) { return; } this.systemMessageHookAddress = RadMessageFilter.SetWindowsHookEx(RadMessageFilter.HookType.WH_MSGFILTER, this.systemMessageDelegate, IntPtr.Zero, currentThreadId); this.installedHook |= InstalledHook.SystemMessage; }
private InstalledHook GetDesiredHook() { InstalledHook installedHook = InstalledHook.None; foreach (IMessageListener listener in this.listeners) { InstalledHook desiredHook = listener.DesiredHook; if ((desiredHook & InstalledHook.CallWndProc) == InstalledHook.CallWndProc) { installedHook |= InstalledHook.CallWndProc; } if ((desiredHook & InstalledHook.GetMessage) == InstalledHook.GetMessage) { installedHook |= InstalledHook.GetMessage; } if ((desiredHook & InstalledHook.SystemMessage) == InstalledHook.SystemMessage) { installedHook |= InstalledHook.SystemMessage; } } return(installedHook); }
private void Uninstall(InstalledHook toUninstall) { if ((toUninstall & InstalledHook.CallWndProc) != InstalledHook.None && (this.installedHook & InstalledHook.CallWndProc) != InstalledHook.None) { RadMessageFilter.UnhookWindowsHookEx(this.callWndProcHookAddress); this.callWndProcHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.CallWndProc; } if ((toUninstall & InstalledHook.GetMessage) != InstalledHook.None && (this.installedHook & InstalledHook.GetMessage) != InstalledHook.None) { RadMessageFilter.UnhookWindowsHookEx(this.getMessageHookAddress); this.getMessageHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.GetMessage; } if ((toUninstall & InstalledHook.SystemMessage) == InstalledHook.None || (this.installedHook & InstalledHook.SystemMessage) == InstalledHook.None) { return; } RadMessageFilter.UnhookWindowsHookEx(this.systemMessageHookAddress); this.systemMessageHookAddress = IntPtr.Zero; this.installedHook &= ~InstalledHook.SystemMessage; }