Search code examples
c++winapiusblibusblibusb-win32

Error getting number of devices using Libusb-win32-1.2.6.0


I am trying to run a simple code written with libusb-win32 on VS2010 to get information about the USB devices connected. I cannot get it run sucessfully.

#include <stdio.h>
#include <string.h>
#include "lusb0_usb.h"

int verbose = 1;

int print_device(struct usb_device *dev, int level);

int main(int argc, char *argv[])
{
    struct usb_bus *bus;

    if (argc > 1 && !strcmp(argv[1], "-v"))
        verbose = 1;

    usb_init();
    usb_set_debug(255);

    int nBusses = usb_find_busses();
    int nDevices = usb_find_devices();
    if(nDevices <=0) return 0;
    for (bus = usb_get_busses(); bus; bus = bus->next)
    {
        if (bus->root_dev && !verbose)
            print_device(bus->root_dev, 0);
        else
        {
            struct usb_device *dev;

            for (dev = bus->devices; dev; dev = dev->next)
                print_device(dev, 0);
        }
    }

    return 0;
}

The project configuration is Win32, debug and I am using the x86 dll & lib files. I am able to compile the code. The usb_find_busses() returns 1 & usb_find_devices() returns 0. I do not understand why I am not getting correct numbers. Printing bus->root_dev prints the following output.

Dev #0: 0000 - 0000
bLength:             18
bDescriptorType:     01h
bcdUSB:              0200h
bDeviceClass:        09h
bDeviceSubClass:     00h
bDeviceProtocol:     00h
bMaxPacketSize0:     40h
idVendor:            0000h
idProduct:           0000h
bcdDevice:           0100h
iManufacturer:       0
iProduct:            0
iSerialNumber:       0
bNumConfigurations:  0
  Couldn't retrieve descriptors

I ran the inf-wizard.exe and there I am able to see all the devices with vendor and device ids. I do not understand what I am missing.


Solution

  • The usb_find_devices() documentation in the libusb 0.1 API says:

    Returns the number of changes since the previous call to this function (total of new device and devices removed).

    So, it does not return the number of available devices, like you are expecting. If you need that value, enumerate the bus list counting the devices yourself. Otherwise, just ignore the count and move on to your bus printing loop, it will run across any connected devices.

    That being said, there is a newer libusb 1.0 API, but its documentation does not mention usb_find_devices() at all. usb_find_devices() appears to have been replaced with a new usb_get_devices_list() function:

    Returns a list of USB devices currently attached to the system.

    This is your entry point into finding a USB device to operate.

    You really should be using the newer 1.0 API, per the libusb website:

    There are several libusb-0.1 API implementations:

    • libusb-0.1 is the very first libusb implementation.
    • libusb-compat-0.1 is a compatibility library which provides the libusb-0.1 API by using the libusb-1.0 API.
    • libusb-win32 is a Windows-only implementation of the libusb-0.1 API. The libusb-win32 project has also created the open source libusb0.sys Windows kernel driver, which exposes a userspace API that allows USB devices to be accessed outside of the Windows kernel.

    ...

    Because the 0.1 and 1.0 APIs use different prefixes they are compatible with each other. It is common that both are installed in parallel on a system. We strongly recommend using libusb-1.0 together with libusb-compat-0.1 instead of the ancient libusb-0.1 code, so that programs which use both the 0.1 API and the 1.0 API in different parts of the program, or in different libraries used by the program, will all use libusb-1.0 for the actual device access. This is important to avoid potential conflicts between libusb-1.0 and libusb-0.1 being used in the same process.

    ... libusb-1.0 is recommended for all new development. Developers are encouraged to port existing applications which use libusb-0.1 to use the new API.

    ...

    libusb-0.1 (legacy API)

    ...

    • Note that libusb-win32 is a separate project which still sees active development. The next generation libusb-win32 kernel driver (libusbk.sys) is based on KMDF. The ​libusbk library will support the existing libusb-win32 API, libusb-1.0 API and WinUSB-like API. libusb-win32 users who are fine with the libusb-win32 API are recommended to keep using it since it will be supported by the libusb-win32 project. libusb-win32 users who are interested in libusb-1.0 will also be supported once the libusbk backend is integrated. Future enhancement of the libusb-1.0 API (say libusb-1.1) may be required to be more suitable for Windows users.