Skip to content

Commit

Permalink
Fix crash due to garbage collected delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifm committed Nov 1, 2022
1 parent 58a3c65 commit 2121469
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions WinAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public WinAPI(ElegantRecorder App)
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

private delegate IntPtr LowLevelHookProc(int nCode, IntPtr wParam, IntPtr lParam);
private LowLevelHookProc mouseDelegate = null;
private LowLevelHookProc keyboardDelegate = null;


public MouseHookStruct CurrentMouseHookStruct;
public MouseHookStruct PrevMouseHookStruct;
Expand All @@ -139,9 +142,13 @@ public WinAPI(ElegantRecorder App)

public void InstallHooks()
{
mouseHookID = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, GetModuleHandle(moduleName), 0);
mouseDelegate = new LowLevelHookProc(MouseHookCallback);

mouseHookID = SetWindowsHookEx(WH_MOUSE_LL, mouseDelegate, GetModuleHandle(moduleName), 0);

keyboardDelegate = new LowLevelHookProc(KeyboardHookCallback);

keyboardHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookCallback, GetModuleHandle(moduleName), 0);
keyboardHookID = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardDelegate, GetModuleHandle(moduleName), 0);
}

public void UninstallHooks()
Expand Down

0 comments on commit 2121469

Please sign in to comment.