7

I am executing the example code of LibUsbDotNet which will return me the information of all connected usb devices. You can find this code below.

using System;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using System.Collections.ObjectModel;

namespace Examples
{
    internal class ShowInfo
    {
        public static UsbDevice MyUsbDevice;

        public static void Main(string[] args)
        {
            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;
            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    Console.WriteLine(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        Console.WriteLine(configInfo.ToString());

                        ReadOnlyCollection<UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            Console.WriteLine(interfaceInfo.ToString());

                            ReadOnlyCollection<UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Console.WriteLine(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }


            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();

            // Wait for user input..
            Console.ReadKey();
        }
    }
}

My problem is that the second line executed in the code:

UsbRegDeviceList allDevices = UsbDevice.AllDevices;

does not not return any device at all, while I do have the device I want to find connected, my keyboard and mouse.

Has anyone encountered this problem before? And/or does anyone know how to solve it?

Thanks in Advance! Milan van Dijck

Milan
  • 556
  • 6
  • 14
  • Is libusb supposed to be a generic device manager API, or just a wrapper around devices that use the libusb-win32 driver? – Ben Voigt Sep 07 '14 at 21:54
  • I am having a similar issue. I was able to connect to the device once, but after that I cannot get the application to connect again... even if I restart the system. – DrBB Mar 16 '20 at 22:07

2 Answers2

4

Does libusb support HID devices?

On Windows, the native Windows HID driver is supported by libusb, but there are some limitations, such as not being able to access HID mice and keyboards, as they are system reserved, as well as getting a direct read of HID report descriptors. Apart from that, you should be communicate with an HID device as you would with any other USB device.

If your application will revolve around HID access, you are encouraged to try to use the ​HIDAPI library by Signal 11 Software, which is also cross-platform. It uses native HID API under Windows and Mac OS X and can use libusb or hidraw as the backend under Linux.

Community
  • 1
  • 1
  • Hmmm, looks like they need to update the page I cited. – Ben Voigt Sep 07 '14 at 23:05
  • Thanks this helped. It seems that the device i have connected is listed as a Human Interface Device as wel thus not generating any output for the UsbDevices.AllDevices list. – Milan Sep 08 '14 at 07:03
  • I am trying to get access to USB-COM port. This is not HID device but UsbDevice.AllDevices return emtpy result. This is odd – Vlad Feb 07 '19 at 14:10
2

The documentation says that

Gets a list of all available USB devices (WinUsb, LibUsb, Linux LibUsb v1.x).

and

Use this property to get a list of USB device that can be accessed by LibUsbDotNet.

If you are using the standard HID driver for your mouse and keyboard and haven't replaced that with the libusb.sys driver, then LibUsbDotNet can't access those devices and therefore doesn't list them.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720