0

My app will be running from a USB key which have specific information like vendor-id, device-id, ... which I need.

I tried using libusb, but despite the fact that I still can't make it work properly, how could I find the right usb drive to get information from?

Note that I would like the code to be cross-platform, that's why I firstly choose libusb.

Edit :

I have found a program (usbviewer) that enumerates all usb ports and the specific informations on the drive when it's connected.

I have read the sources for linux, infos are directly extracted from /proc/bus/usb/devices, it's quite straightforward.

But on windows, to get usb node connection information, it takes about 1000 lines before you get the actual information in a data structure (enumerate hub controllers, root hubs, ports...).

libusb on the otherside force me to generate an INF file and install a driver via inf-wizard.exe for each device before I can reach the information (didn't try on linux though).

Edit 2 :

I have found a way to get the device serial number with Windows, Linux and Mac OS X (didn't try MAC OS X but it should work just as Linux). For Windows, I use the function GetVolumeInformation(), on Linux, to read the serial number of drives with a FAT filesystem, I read 4 bytes in the corresponding /dev/* file starting from 0x27 or 0x43 depending on the FAT version.

But this reading requieres a root access which the program won't have, and it seems to me a little bit unreliable.

Any ideas ?

asked May 10, 2012 at 13:27
6
  • 2
    Cross-platform meaning which platforms? I guess you don't care about MVS, so it probably includes some Windows versions, Linux, OS X? Android? iOS? Commented May 10, 2012 at 13:30
  • 1
    if you take main()'s arguments of argc and argv, the first argument always points toward (the command used to execute) your executable, with full path information. from there, you can use something like libusb Commented May 10, 2012 at 13:31
  • Windows, Linux and OS X, it won't be used by mobile devices. How can the path link me to a libusb data structure device ? Commented May 10, 2012 at 13:36
  • @std''OrgnlDave The standard does say that it should be (if it's available) but apparently that's violated with enough regularity. See this thread: stackoverflow.com/questions/5217395/… Commented May 10, 2012 at 13:50
  • no answers regarding my problem ? I managed to use libusb-win32, but I have to install a driver via inf-wizard.exe (in admin mode...) each time I have a new usb device Commented May 11, 2012 at 7:19

1 Answer 1

0

I have found the solution that fits me, for thoses who are insterested.

For Windows :

I extract the serial number of the device from the registry knowing the drive letter : HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices\DosDevices\X:

For Unix :

I get the /dev file with the df command, then I read the link in /sys/block corresponding to the /dev file (e.g : The drive is /dev/sdb, I read link of /sys/block/sdb. Then I read the file serial which is located in the directory pointed by the link, 6x directory backwards.

Hope it helps.

answered May 23, 2012 at 8:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.