2

I need to list existing USB hub and the devices connected in the hub using my C++ program.

I can able to print the USB Hub and devices connected in hub from terminal using the commands

lsusb lsusb -v

I want to use that feature in my C++ program.

How I can do this programmatically. Is there any C++ classes available to use in my Qt application or in c or java.this is one help taken from https://unix.stackexchange.com/questions/61484/find-the-information-of-usb-devices-in-c

#include <stdio.h>
#include <usb.h>
main(){
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
    for (dev = bus->devices; dev; dev = dev->next){
        printf("Trying device %s/%s\n", bus->dirname, dev->filename);
        printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
        printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
    }

} does anyone know how to compile this??? i m facing problem

Community
  • 1
  • 1
Srb
  • 219
  • 3
  • 13

2 Answers2

1

You need to install libusb-dev and libusb

sudo apt-get update
sudo apt-get install libusb
sudo apt-get install libusb-dev

After that compile your code with -lusb

gcc usbtest.c -o usbtest -lusb
Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
  • have u done this type of stff i.e to know the connected devices on linux machine – Srb Apr 24 '14 at 08:50
  • thanx now its working...can u share some sample code as i have to show in my company..thanx in advance – Srb Apr 24 '14 at 08:59
  • this program is showing many usb devices...i want to know only about mouse,keyboard i.e external devices....can u help me in this – Srb Apr 24 '14 at 09:38
0

Check this: How to execute a command and get output of command within C++ using POSIX?

You could use system() if you do not need the stdout/stderr.

You could use popen() if you want to capture the stdout.

You could use execlp() to run the command you desire. The function call for your question would be something like execlp("ls","Isusb","Isusb","-v")
Read this for more information: http://linux.about.com/library/cmd/blcmdl3_execlp.htm

Community
  • 1
  • 1
Sinstein
  • 887
  • 11
  • 42