tags:

views:

145

answers:

3

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"
+1  A: 

The following example uses WMI:

http://www.emmet-gray.com/Articles/USB_SerialNumbers.htm

Learn how to open WMI objects with ATL in C++, and you will be able to transcode this easily.

Pavel Radzivilovsky
A little further down the page there's a Win32 API method that should be easily translatable to C++ if you don't like to use the WMI COM objects.Nice article btw, tnx.
Eugen Constantin Dinca
+3  A: 

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
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
I'll save that answer for your customers, gets me more upvotes :)
Hans Passant
@Hans Passant: 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
Hmya, what can I say. I don't have deep knowledge about how instance IDs get generated, never tried to crack a program that relied on it. Not my style. All I can say is: if you want to trust on security by obscurity then make sure that you'll never be successful at what you do. If you really want to find out then post at the cracker forums, you'll find people that know all the tricks of breaking weak security schemes. SO isn't it. The usual dismissal is that if you can't afford a *real* solution then your product isn't worth the effort in the first place.
Hans Passant
+1  A: 

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

MSalters