{"record":{"id":"572ca7d35e459694","repo":"babalae/better-genshin-impact","slug":"raw-input-572ca7","errorCode":null,"errorMessage":"注册 Raw Input 鼠标设备失败","messagePattern":"注册 Raw Input 鼠标设备失败","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Monitor/RawInputMonitor.cs","lineNumber":328,"sourceCode":"    private void RegisterRawInput(RawInputThreadContext context)\n    {\n        var devices = new[]\n        {\n            new User32.RAWINPUTDEVICE\n            {\n                usUsagePage = GenericDesktopUsagePage,\n                usUsage = MouseUsage,\n                dwFlags = User32.RIDEV.RIDEV_INPUTSINK,\n                hwndTarget = context.HwndSource!.Handle\n            }\n        };\n\n        if (!User32.RegisterRawInputDevices(\n                devices,\n                (uint)devices.Length,\n                (uint)Marshal.SizeOf<User32.RAWINPUTDEVICE>()))\n        {\n            throw new Win32Exception(Marshal.GetLastWin32Error(), \"注册 Raw Input 鼠标设备失败\");\n        }\n    }\n\n    private void CleanupContext(RawInputThreadContext context)\n    {\n        if (context.Registered)\n        {\n            var devices = new[]\n            {\n                new User32.RAWINPUTDEVICE\n                {\n                    usUsagePage = GenericDesktopUsagePage,\n                    usUsage = MouseUsage,\n                    dwFlags = User32.RIDEV.RIDEV_REMOVE,\n                    hwndTarget = HWND.NULL\n                }\n            };\n","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Monitor/RawInputMonitor.cs#L310-L346","documentation":"RegisterRawInputDevices (the Win32 API) returns false when registration fails; RawInputMonitor throws a Win32Exception built from Marshal.GetLastPInvokeError(). The device registered is the mouse with RIDEV_INPUTSINK (receive input even when not foreground) bound to the message-only HWND. Failure typically means the HWND target is invalid or the session disallows raw input.","triggerScenarios":"context.HwndSource.Handle is zero/disposed at registration time; running in a non-interactive session; another process holds an incompatible raw-input registration; insufficient privileges for INPUTSINK.","commonSituations":"RDP/session transitions disposing the HWND; rapid Start/Stop where the HWND was torn down; headless/service hosting; security software blocking raw input.","solutions":["Inspect the NativeErrorCode in the Win32Exception to map the exact Win32 failure.","Ensure Start() is not racing with Stop()/Dispose() that destroys the HWND.","Run in an interactive desktop session; fall back to DirectInput if unavailable.","Retry registration after recreating the HWND."],"exampleFix":"// before\nmonitor.Start();\n\n// after\ntry\n{\n    monitor.Start();\n}\ncatch (Win32Exception ex) when (ex.Message.Contains(\"注册 Raw Input 鼠标设备失败\"))\n{\n    logger.LogError(\"RawInput 注册失败 Win32Error={Code}\", ex.NativeErrorCode);\n    throw;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    monitor.Start();\n}\ncatch (System.ComponentModel.Win32Exception ex) when (ex.Message.Contains(\"注册 Raw Input 鼠标设备失败\"))\n{\n    logger.LogError(\"RawInput 注册失败 Win32Error={Code}\", ex.NativeErrorCode);\n    // fall back to DirectInput if available\n}","preventionTips":["Map ex.NativeErrorCode to understand the Win32 failure reason.","Prevent Start/Stop races that dispose the HWND mid-registration.","Run in an interactive session; fall back to DirectInput otherwise."],"tags":["rawinput","win32","win32exception","monitor","mouse"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}