gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.04k stars 257 forks source link

Overflow Exception #164

Open EgilSandfeld opened 1 year ago

EgilSandfeld commented 1 year ago

Had a crash from one end-user of my app: Any ideas how this was caused?

.NET Framework 4.8 app

Arithmetic operation resulted in an overflow,

   at System.IntPtr.op_Explicit(IntPtr value)
   at Gma.System.MouseKeyHook.KeyEventArgsExt.FromRawDataGlobal(CallbackData data)
   at Gma.System.MouseKeyHook.Implementation.GlobalKeyListener.GetDownUpEventArgs(CallbackData data)
   at Gma.System.MouseKeyHook.Implementation.KeyListener.Callback(CallbackData data)
   at Gma.System.MouseKeyHook.WinApi.HookHelper.HookProcedure(Int32 nCode, IntPtr wParam, IntPtr lParam, Callback callback)
   at Gma.System.MouseKeyHook.WinApi.HookHelper.<>c__DisplayClass7_0.<HookGlobal>b__0(Int32 code, IntPtr param, IntPtr lParam)

"targetSite": "Int32 op_Explicit(IntPtr)"
"type": "System.OverflowException"
EgilSandfeld commented 1 year ago

The crash log above happened within 1 second of the app starting up, however I just received one now, with the similar stacktrace happened 34170 seconds into the app session..

Calling code to set up subscribers:

private void SubscribeKeyboard()
        {
            try
            {
                if (_appKeyboard == null)
                {
                    _appKeyboard = Hook.AppEvents();
                    _appKeyboard.KeyDown += AppKeyboardDown;
                }

                if (_keyboard == null)
                {
                    _keyboard = Hook.GlobalEvents();
                    _keyboard.KeyPress += KeyboardKeyPress;
                    _keyboard.KeyDown += (sender, args) => Core.Dispatcher.OnKeyboardDown?.Invoke(sender, args);
                    _keyboard.KeyUp += (sender, args) => Core.Dispatcher.OnKeyboardUp?.Invoke(sender, args);
                }
            }
            catch (Exception ex)
            {
                DB.AddBugInfo(ex);
            }
        }

I believe Hook.GlobalEvents(); call is the one facilitating the later crashes.

EgilSandfeld commented 1 year ago

Can a missing System.Reactive.dll be the cause of this? So when the user presses a key, the missing dll causes the above stack?

EgilSandfeld commented 1 year ago

Attempted a fix in https://github.com/gmamaladze/globalmousekeyhook/compare/vNext...EgilSandfeld:globalmousekeyhook:issue-164-fix