windows 获取USB序列号C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3096823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:42:09  来源:igfitidea点击:

Get USB serial number c++

c++windowsusb

提问by Steven smethurst

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

我希望在 c++ 中从 Windows 上的 USB 记忆棒中检索序列号。

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

我发现了很多 C# 示例,以及用于 linux 的 c++,但没有用于 c++ 和 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"
  • 不幸的是,C++/CLI 不是一个选项。
  • 我正在寻找设备的序列号而不是文件系统。或“设备实例路径”又名“InstanceID”

采纳答案by Hans Passant

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.

如果需要 C++,则您必须编写 COM 代码。与VB.NET代码相比有点不爽,但是MSDN库中有非常好的示例,您只需要调整查询即可。

Beware that the example you linked to does notreturn 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.

请注意,您链接的示例不会返回设备本身的序列号,只会返回设备上的文件系统。USB 设备没有序列号。

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.

文件系统序列号很容易复制,以防您正在考虑编写某种许可实施方案。如果这很重要,那么您应该使用专为此目的设计的 USB 设备。一个加密狗。无法破解。制造商将提供用于验证许可证的 C++ 代码。

回答by Rafael Baptista

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

我有一篇关于如何在不使用 WMI 的情况下在 C++ 中获取卷的 USB 序列号的文章

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

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.

这样做有一个技巧,您必须将 USB 设备与磁盘卷匹配。如果可以,您希望避免 WMI,因为它不可靠。

回答by MSalters

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

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