收录日期:2019/04/20 16:50:33 时间:2010-06-22 20:42:15 标签:c++,windows,usb

I'm looking to retrieve the serial number from a USB memory stick on windows in c++.

I have found plenty of C# examples, and c++ for linux but none for c++ and windows.

Edit

  • C++/CLI is not an option unfortunately.
  • I am looking for the serial number of the device not the file system. OR "Device Instance Path" AKA "InstanceID"

If C++ is a requirement then you'll have to write COM code. A bit unjoyful compared to VB.NET code, but there are very good examples available in the MSDN library, you just need to adapt the query.

Beware that the example you linked to does not return a serial number of the device itself, just the file system on the device. Which is as good as it is going to get it, USB devices don't have serial numbers.

File system serial numbers are trivial to duplicate, in case you are contemplating this to write some kind of licensing enforcement scheme. If that's important then you should use a USB device that was designed for that purpose. A dongle. Impossible to crack. And C++ code to verify the license will be available from the manufacturer.

I have an article about how to get USB serial numbers for volumes in C++, without using WMI

http://oroboro.com/usb-serial-number/

There is a trick to doing it, you have to match up USB devices with disk volumes. You want to avoid WMI if you can because it is unreliable.

The serial number is the subkey under HKLM\SYSTEM\CCS\Enum\USB\Vid_xxxx&Pid_yyyy