Search code examples
c#usbhidjoystick

Getting Error: System.Exception: Could not get device interface detail. Error code: 122 when using HID.Net and USB.Net


I have a Console App (.Net Core) project that is using the Hid.Net Library, where the end goal is to connect with my HID game controllers and read data from it, but as a starting point in understanding the library, I had written code to enumerate connected devices of the game controller I wanted to connect to. The code I had written was following the documentation from the library. But when I ran the project, I got the error:

System.Exception: Could not get device interface detail. Error code: 122

This is the code that I had written:

using System;
using System.Threading.Tasks;
using Device.Net;
using Hid.Net.Windows;
using Usb.Net.Windows;

namespace IOLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckforDevices();

            Console.ReadLine();
        }

        static async Task CheckforDevices()
        {
            try
            {
                WindowsHidDeviceFactory.Register();
                WindowsUsbDeviceFactory.Register();

                var devices = await DeviceManager.Current.GetConnectedDeviceDefinitions(0x0079, 0x0006);
                foreach(var device in devices)
                {
                    Console.WriteLine(device.DeviceId);
                }

            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

I don't understand what I am doing wrong, so any explanation or insight is greatly appreciated.


Solution

  • Edit: This bug has been fixed and is in the current 2.3 release. However, it is still worth cloning the repo and stepping through the code if you think that a device is not being returned. A device won't returned if something goes wrong during the process of trying to collect information about it

    Previous Answer: This is a known bug. Sometimes, the calls to get Hid details don't work and so the rest will fail. I've actually fixed this issue in this commit 664978f5084a6113a2b9e9a0371d5c7d40f30731, but I believe the fix is only in the develop branch. If you checkout that revision, the code should not crash. I will put a new NuGet version up to fix the issue soon. However, you should just understand that if Hid.Net can't get the details of the device, it won't consider it connected.