From 5f9036568d53a01b6b0c64be94c3aeb0afad3028 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Sun, 5 Dec 2021 21:04:41 -0600 Subject: [PATCH] Use Safe Delegate Instead --- Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs | 7 ++++--- Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs b/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs index 5f14b7598..49efc91b8 100644 --- a/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs +++ b/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs @@ -12,8 +12,8 @@ namespace Flow.Launcher.Infrastructure.Hotkey public unsafe class GlobalHotkey : IDisposable { private static readonly IntPtr hookId; - - + + private static InterceptKeys.LowLevelKeyboardProc _hookedKeyboardProc; public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state); internal static Func hookedKeyboardCallback; @@ -26,8 +26,9 @@ namespace Flow.Launcher.Infrastructure.Hotkey static GlobalHotkey() { + _hookedKeyboardProc = LowLevelKeyboardProc; // Set the hook - hookId = InterceptKeys.SetHook(& LowLevelKeyboardProc); + hookId = InterceptKeys.SetHook(_hookedKeyboardProc); } public static SpecialKeyState CheckModifiers() diff --git a/Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs b/Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs index 7fec3c383..1203eca42 100644 --- a/Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs +++ b/Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs @@ -10,7 +10,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey private const int WH_KEYBOARD_LL = 13; - public static IntPtr SetHook(delegate* proc) + public static IntPtr SetHook(LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) @@ -20,7 +20,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern IntPtr SetWindowsHookEx(int idHook, delegate* lpfn, IntPtr hMod, uint dwThreadId); + public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)]