使用 Libusb-win32-1.2.6.0 获取设备数量时出错

Error getting number of devices using Libusb-win32-1.2.6.0

我正在尝试 运行 在 VS2010 上使用 libusb-win32 编写的简单代码来获取有关连接的 USB 设备的信息。我没能成功运行

#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;
}

项目配置为Win32,调试,我使用的是x86 dll & lib文件。我能够编译代码。 usb_find_busses() returns 1 & usb_find_devices() returns 0。我不明白为什么我没有得到正确的数字。打印 bus->root_dev 打印以下输出。

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 运行 inf-wizard.exe 在那里我可以看到所有具有供应商和设备 ID 的设备。我不明白我错过了什么。

usb_find_devices() documentation in the libusb 0.1 API 说:

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

因此,它并不像您期望的那样 return 可用设备的数量。如果您需要该值,请自己枚举计算设备的总线列表。 否则,只需忽略计数并继续您的总线打印循环,它将 运行 跨越所有连接的设备。

也就是说,有一个更新的 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() 函数:

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

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

根据 libusb website:

,您确实应该使用较新的 1.0 API

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.