1

I'm trying to learn USB Handling on Windows 10 using C and libusb-1.0. for starters I was able to initialize the library successfully using the libusb_init() function. I have verified the initialization with the return code as 0. Next I tried using the libusb_open_device_with_vid_pid() function and stored the return value of this function in the provided libusb_device_handle struct. For some reason the return value of this function is always NULL. I have tried printing the device list with the libusb_get_device_list() function and my Arduino Nano is being detected there with the correct VID and PID (Verified from Windows Device Manager).

Can anyone tell me what could be going wrong here?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

#include "include/libusb-1.0/libusb.h"

#define VID     0x1A86
#define PID     0x7523

int main(void)
{
    int status = 0;

    libusb_device_handle* dev = NULL;

    system("cls");

    printf("Initializing libusb...\n");

    status = libusb_init(NULL);

    if(status != 0)
    {
        printf("\nCannot Initialize libusb!!\n");
        
        return 0;
    }
    printf("\nlibusb Initialized Successfully...\n");

    dev = libusb_open_device_with_vid_pid(NULL, VID, PID);
    
    if(dev == NULL)
    {
        printf("\nError!! Could Not Find USB Device!\n");
        libusb_exit(NULL);
        return 0;
    }
    printf("\nUSB Device Found...\n");

    libusb_exit(NULL);

    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

0

Try to run your code as administrator, to do that you can do the following:

  1. compile your code as you currently do
  2. click on the "Microsoft" key in the keyboard or the start menu.
  3. in the search bar type: "cmd" (don't press enter)
  4. on the right to where you typed the command you will have a context menu, click on "Run as administrator"
  5. go to the directory where your compiled program is saved (a .exe file) using the cd command
  6. type the .exe file name (with or without the .exe suffix) and press enter

if this solves your problem, it means that your problem is not running the program with admin permissions.

Gal Weiss
  • 279
  • 1
  • 9
  • I tried doing that but the result is still the same. I even tried using the `libusb_get_device_list()` function to first find my device (this works) and then use the `libusb_open()` function to open the device but that does not work either. – Devashish Lahariya Apr 21 '23 at 09:30