Search code examples
visual-studiolibusb

libusb can't get device handle


#include <windows.h>
#include <devguid.h>
#include <initguid.h>
#include <wtypes.h>
#include <WinBase.h>
#include <stdio.h>
#include "libusb.h"
#include "resource.h"

int main(int argc, char *argv[]) {

    libusb_device *dev;
    int i = 0, j = 0;
    libusb_device **devs;
    int r;
    ssize_t cnt;
    USHORT VendorID = 0x04b4;
    USHORT ProductID = 0x00f3;
    libusb_device_handle * DeviceHandle;

    AllocConsole();

    r = libusb_init(NULL);
    if (r < 0)
        return r;

    cnt = libusb_get_device_list(NULL, &devs);
    if (cnt < 0)
        return (int)cnt;

    while ((dev = devs[i++]) != NULL) {
        struct libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0) {
            fprintf(stderr, "failed to get device descriptor");
            return 1;
        }
        if (desc.idVendor == VendorID &&
            desc.idProduct == ProductID){

            int err = libusb_open(dev,
                &DeviceHandle);
            if (err < 0){
                printf("GetLastError : %d\n", GetLastError());
                return 1;
                }
            libusb_free_device_list(devs, 1);

            libusb_close(DeviceHandle);
        }
    }
    return 0;
}

Here is my code. Problem is that I can't get device handle. &DeviceHandle is just a lot of 0 and get last error doesn't give me anything. If it is important I try to take handle to cypress fx3. It compiles and works perfectly until I go to this point. Any thoughts about this problem are welcome.


Solution

  • Your call to libusb_open(dev, &DeviceHandle); returns LIBUSB_ERROR_NOT_SUPPORTED.

    This post suggests you may be missing the winusb driver.