1

Topic: HidSharp: Occasional ArgumentException thrown by HidManager.GetDevices

Hi,

My details:

  • Windows 7 64-bit

  • HidSharp: 1.5

  • .NET Framework 4 Client Profile

  • Building a 32-bit WinForms program

I'm hitting an intermittent ArgumentException in HidManager.GetDevices. The following line in that method:

lock (_deviceList) { _deviceList.Add(addition_, device); }

occasionally throws an ArgumentException saying that the key already exists. My set up is a WinForms program that has the following method in the form class:

    private const int WM_DEVICECHANGE = 0x0219;
    private const int DBT_DEVNODES_CHANGED = 0x0007;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == MainForm.WM_DEVICECHANGE && m.WParam.ToInt32() == DBT_DEVNODES_CHANGED)
        {
            HidDeviceLoader loader = new HidDeviceLoader();
            foreach (HidDevice device in loader.GetDevices())
            {
                // Do something
            }
        }
    }

This code can be triggered multiple times in rapid succession which might be relevant. If I run my program and repeatedly plug and unplug a USB device eventually the ArgumentException occurs.

I added some Console.WriteLines to HidManager.GetDevices which produced the following output. It appears that something else is modifying _deviceList between the start of GetDevices and the point at which _deviceList.Add is called. Maybe there are threads still running from a previous call to GetDevices, I'm not sure:

    _deviceList.Keys (start of GetDevices):
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col01#7&1d798885&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col01#7&1f93e0c0&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&pid_0002#6&145c4476&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col02#7&1f93e0c0&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_00#7&7d0547b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col02#7&1d798885&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col03#7&1d798885&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col04#7&1d798885&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_01#7&30a9a4c3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    devices (after catching the ArgumentException):
    \\?\hid#vid_045e&pid_028e&ig_00#7&1f6318a&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_00#7&7d0547b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col01#7&1f93e0c0&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col02#7&1f93e0c0&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&pid_0002#6&145c4476&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col01#7&1d798885&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col02#7&1d798885&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col03#7&1d798885&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col04#7&1d798885&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_01#7&30a9a4c3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    additions (after catching the ArgumentException):
    \\?\hid#vid_045e&pid_028e&ig_00#7&1f6318a&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    removals (after catching the ArgumentException):
    _deviceList.Keys:
    \\?\hid#vid_045e&pid_028e&ig_00#7&1f6318a&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col01#7&1d798885&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col01#7&1f93e0c0&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&pid_0002#6&145c4476&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_01&col02#7&1f93e0c0&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&pid_1818&mi_00#7&7d0547b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col02#7&1d798885&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col03#7&1d798885&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_00&col04#7&1d798885&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&pid_2e22&mi_01#7&30a9a4c3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    addition_ (after catching the ArgumentException):
    \\?\hid#vid_045e&pid_028e&ig_00#7&1f6318a&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

I've worked around this problem in my code now so it's not an issue for me but I thought I would report the problem anyway. To Zer: thank you for HidSharp, other than this one issue it has been absolutely perfect, just what I was looking for.