简体   繁体   中英

Get USB serial number c++

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"

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.

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.

序列号是HKLM\\SYSTEM\\CCS\\Enum\\USB\\Vid_xxxx&Pid_yyyy下的子项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM