I want to list all connected devices programatically.Basically,what i am trying to do is to check how many devices are connected at USB port and PS2 port.
Asked
Active
Viewed 2,750 times
1
-
See http://stackoverflow.com/questions/13927475/windows-how-to-enumerate-all-connected-usb-devices-device-path/13928035#13928035 for enumeration of USB devices. – Michael Jan 07 '15 at 12:49
1 Answers
1
You can use SetupDiEnumDeviceInterfaces and SetupDiGetDeviceInterfaceDetail (http://msdn.microsoft.com/en-us/library/windows/hardware/ff551120%28v=vs.85%29.aspx) to get the details on windows.
Here's a sample code: https://gist.github.com/rakesh-gopal/9ac217aa218e32372eb4
SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE,dwMemberIdx, DevIntfData);
SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevIntfData, NULL, 0, &dwSize, NULL);
The idea is to repeatedly call SetupDiEnumDeviceInterfaces to get all the connected devices and then call SetupDiGetDeviceInterfaceDetail to get more details about the connected device.
You can also use the libusb to get an output similar to lsusb on unix-like systems. Remember to compile with -lusb flag on.
Here's code using libusb: https://gist.github.com/rakesh-gopal/12d4094e2b882b44cb1d
#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);
}
}

Rakesh Gopal
- 580
- 5
- 11
-
-
1@sonugupta The standard C specification doesn't provide APIs to list or interact with USB devices, if that's what you want. You should use a library to deal with USB. BTW, I'll edit my answer to include a solution for Unix-like systems. – Rakesh Gopal Jan 07 '15 at 15:16