stride3d/stride · error · Exception

Failed to register a mouse device for raw input

Error message

Failed to register a mouse device for raw input

What it means

RawInputMouse.Start registers the HID mouse device with the Windows Raw Input API via RawInput.RegisterDevice using RIDEV_INPUTSINK so mouse input is delivered to the target window's message filter. If the underlying RegisterRawInputDevices Win32 call fails, the library throws this exception, aborting mouse initialization.

Solutions

  1. Ensure the target window is created and its handle is valid (non-IntPtr.Zero) before calling Start().
  2. Verify the window handle belongs to the calling thread's process and the window is not already destroyed.
  3. Check for UIPI/elevation mismatch between the process and the foreground window and run at a consistent privilege level.
  4. Retry registration after the window handle changes (e.g. after recreating the window/device).

Example fix

// before
mouse.Start(); // targetWindow not yet created
// after
window.Create();
mouse.Start(); // now targetWindow is a valid HWND
Defensive patterns

Strategy: try-catch

Validate before calling

if (targetWindow == IntPtr.Zero) throw new InvalidOperationException("Start raw input only after the window handle is created");

Type guard

bool CanStart(RawInputMouse m) => m != null && targetWindow != IntPtr.Zero;

Try / catch

try { mouse.Start(); } catch (Exception ex) when (ex.Message.Contains("Failed to register a mouse device")) { Log.Error(ex); /* fall back to non-raw input or recreate window */ }

Prevention

When it happens

Trigger: Calling Start() when RegisterRawInputDevices fails — e.g. targetWindow handle is invalid/zero, the window was already destroyed, RIDEV_INPUTSINK target rules are violated, or the system rejects the device registration.

Common situations: Game window not yet created or disposed before Start; passing an IntPtr.Zero target window with RIDEV_INPUTSINK; running in restricted desktop sessions (RDP, service accounts, elevated/un-elevated UIPI mismatches).

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/03b033ed899178ef. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Input/Windows/RawInput/RawInputMouse.cs:33

        private readonly IntPtr targetWindow;

        public event EventHandler<RawInputMouseEventArgs> events;

        private readonly RawInputMessageFilter messageFilter;
        private bool disposedValue;

        public RawInputMouse(IntPtr targetWindow)
        {
            this.targetWindow = targetWindow;
            this.messageFilter = new RawInputMessageFilter(this.HandleInputMessage, null, null);
        }

        public void Start()
        {
            var result = RawInput.RegisterDevice(UsagePage.HID_USAGE_PAGE_GENERIC, UsageId.HID_USAGE_GENERIC_MOUSE, ModeFlags.RIDEV_INPUTSINK, targetWindow);
            if (!result)
            {
                throw new Exception("Failed to register a mouse device for raw input");
            }

            Application.AddMessageFilter(messageFilter);
        }

        public void End()
        {
            Application.RemoveMessageFilter(messageFilter);

            var result = RawInput.RegisterDevice(UsagePage.HID_USAGE_PAGE_GENERIC, UsageId.HID_USAGE_GENERIC_MOUSE, ModeFlags.RIDEV_REMOVE, IntPtr.Zero);
            if (!result)
            {
                throw new Exception("Failed to unregister a mouse device for raw input");
            }
        }

        public void HandleInputMessage(RawInput.RawMouse mouseInput)
        {

View on GitHub (pinned to 96fad776d2)