{"record":{"id":"5d277298ab97854b","repo":"seerge/g-helper","slug":"hid-device-was-not-found-on-your-machine","errorCode":null,"errorMessage":"HID device was not found on your machine.","messagePattern":"HID device was not found on your machine\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs","lineNumber":20,"sourceCode":"using HidSharp;\n\nnamespace GHelper.AnimeMatrix.Communication.Platform\n{\n    internal class WindowsUsbProvider : UsbProvider\n    {\n        protected HidDevice HidDevice { get; }\n        protected HidStream HidStream { get; }\n\n        public WindowsUsbProvider(ushort vendorId, ushort productId, string path, int timeout = 500) : base(vendorId, productId)\n        {\n            try\n            {\n                HidDevice = DeviceList.Local.GetHidDevices(vendorId, productId)\n                   .First(x => x.DevicePath.Contains(path));\n            }\n            catch\n            {\n                throw new IOException(\"HID device was not found on your machine.\");\n            }\n\n            var config = new OpenConfiguration();\n            config.SetOption(OpenOption.Interruptible, true);\n            config.SetOption(OpenOption.Exclusive, false);\n            config.SetOption(OpenOption.Priority, 10);\n            HidStream = HidDevice.Open(config);\n            HidStream.ReadTimeout = timeout;\n            HidStream.WriteTimeout = timeout;\n        }\n\n        public WindowsUsbProvider(ushort vendorId, ushort productId, int maxFeatureReportLength, string name = \"Matrix\")\n            : base(vendorId, productId)\n        {\n            try\n            {\n                HidDevice = DeviceList.Local\n                    .GetHidDevices(vendorId, productId)","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/seerge/g-helper/blob/b9ba417f1b822110ce41a1e152031a84bfa6ad80/app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs#L2-L38","documentation":"Thrown by the WindowsUsbProvider(vendorId, productId, path, timeout) constructor when it cannot find a matching HID device. HidSharp enumerates devices via DeviceList.Local.GetHidDevices(vendorId, productId); this overload selects a device with LINQ .First(x => x.DevicePath.Contains(path)), which throws InvalidOperationException when the filtered sequence is empty. A bare catch swallows that and rethrows it as IOException, hiding the real cause. This overload is used by peripherals (AsusMouse.SetProvider, AsusMouse.cs:537) to identify a device by VID/PID plus a device-path interface substring (e.g. 'mi_02&col01').","triggerScenarios":"Constructing 'new WindowsUsbProvider(vendorId, productId, path, timeout)' when DeviceList.Local.GetHidDevices(vendorId, productId) returns either no devices, or devices whose DevicePath does not contain the supplied path substring. The internal .First() throws InvalidOperationException (sequence contains no elements); the catch converts it to IOException. It is NOT thrown by a feature-report-length mismatch (that is error [1]) nor by a failure to open the stream.","commonSituations":"The mouse/peripheral is disconnected, switched off, or asleep; the VID/PID hardcoded in the subclass do not match the plugged device; the device enumerated under a different HID interface so the path substring no longer matches; another program (e.g. Armoury Crate) holds the interface exclusively so HidSharp does not list it; USB cable/hub issue; HidSharp enumeration returned stale results right after a hot-plug. Developers also hit this after changing the path filter or port the device is plugged into.","solutions":["Confirm the peripheral is connected, powered on, and visible in Windows Device Manager under Human Interface Devices.","Verify the VID/PID passed to the constructor match the actual device (inspect with a HID enumeration tool or HidSharp DeviceList.Local.GetHidDevices).","Print all candidate DevicePath strings for the VID/PID and confirm the 'path' substring matches one of them.","Close other software that may hold the device exclusively (Armoury Crate, SignalRGB), then retry.","Unplug and replug the device (or toggle it off/on) so HidSharp re-enumerates, then reconstruct the provider.","Replace .First() with .FirstOrDefault() and a null check so the real reason (no path match) is reported instead of a generic message."],"exampleFix":"// before\nHidDevice = DeviceList.Local.GetHidDevices(vendorId, productId)\n   .First(x => x.DevicePath.Contains(path));\n// after\nHidDevice = DeviceList.Local.GetHidDevices(vendorId, productId)\n   .FirstOrDefault(x => x.DevicePath.Contains(path));\nif (HidDevice is null)\n    throw new IOException($\"HID device VID=0x{vendorId:X4} PID=0x{productId:X4} path='{path}' was not found on your machine.\");","handlingStrategy":"validation","validationCode":"// Mirror the constructor's selection logic before building the provider.\nbool IsMousePresent(ushort vid, ushort pid, string path) =>\n    HidSharp.DeviceList.Local.GetHidDevices(vid, pid)\n        .Any(d => d.DevicePath.Contains(path));\n\n// Use it before constructing:\n// if (!IsMousePresent(_vendorId, _productId, path)) { Logger.WriteLine(\"mouse not found\"); return; }","typeGuard":null,"tryCatchPattern":"// The constructor throws IOException on a missing device, so guard the caller:\ntry\n{\n    _usbProvider = new WindowsUsbProvider(_vendorId, _productId, path, USBTimeout());\n}\ncatch (IOException ex)\n{\n    Logger.WriteLine($\"{GetDisplayName()} connect failed: {ex.Message}\");\n    _usbProvider = null; // downstream Set/Get already null-check _usbProvider\n}","preventionTips":["Pre-validate device presence with a GetHidDevices(...).Any(...) predicate before constructing the provider (AsusMouse.IsDeviceConnected at AsusMouse.cs:512 is the existing pattern to copy).","Subscribe to HidSharp.DeviceList.Local.Changed to reconstruct providers only after a real enumeration event, avoiding stale-state construction.","Never hardcode fragile path substrings without also logging all candidate DevicePath values when a device fails to match.","Treat a missing peripheral as an expected runtime state, not an exceptional one: keep _usbProvider nullable and let Set/Get no-op (the base Device class already does this).","Replace .First() with .FirstOrDefault() in the provider so a null check can produce a precise message instead of a swallowed InvalidOperationException."],"tags":["hid","usb","hidsharp","hardware","windows","peripherals","device-enumeration"],"backgroundTag":null,"analyzedSha":"b9ba417f1b822110ce41a1e152031a84bfa6ad80","analyzedAt":"2026-08-13T15:05:32.228Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}