4

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"
Steven Smethurst
  • 4,495
  • 15
  • 55
  • 92
  • 1
    If C++/CLI is an option, you could use the same method as the existing C# examples. Or, you could post a link to one of the .NET (C#) examples and somebody might know a way to translate that to Win32 (C) for you. – Greg Hewgill Jun 22 '10 at 20:23
  • The C# examples would be translatable. Are you talking about the serial number of the device or the filesystem? – Yann Ramin Jun 22 '10 at 20:25
  • This question shows the .net solution http://stackoverflow.com/questions/450009/how-to-get-serial-number-of-usb-stick-in-c – danatel Jun 22 '10 at 20:34
  • 1
    @danatel: There's a link to a Win32 example right on that same page. – Greg Hewgill Jun 22 '10 at 20:35
  • @Greg Hewgill: The example is for VB.net not c++ – Steven Smethurst Jun 22 '10 at 20:44
  • 1
    @Steven, it might be in VB, but it uses the Win32 API or the WMI API. Which is available from C++. Just use msdn.com and search for the functions used in the examples. – jpyllman Jun 22 '10 at 20:48
  • The number of threads in the past year where the OP, challenged with writing fundamentally unmanaged code, can only find C# examples is growing rapidly. Wow, C# is kicking C++'s ass big time. – Hans Passant Jun 22 '10 at 21:21

3 Answers3

6

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.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Yes thats exactly what I was going to use this Serial number for. A licensing system. How would you go about changing a "File system serial numbers"? – Steven Smethurst Jun 22 '10 at 21:24
  • Are you talking about the GetVolumeInformation(), I am aware that this bit if information changes everytime the drive is format. Utility to change it on the fly: http://www.codeproject.com/kb/system/change_drive_sn.aspx But I have not heard of anything that change change the "Device Instance Path" OR the "InstanceID" of a device. – Steven Smethurst Jun 22 '10 at 21:35
  • 4
    Most USB devices do have serial numbers. If you want the actual device serial number, you have to match up USB devices with disk volumes. See my answer and a link to an article about how to do it below. – Rafael Baptista Aug 05 '12 at 19:18
1

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

MSalters
  • 173,980
  • 10
  • 155
  • 350