سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top