使用WMI获取系统硬盘驱动器的制造商序列号

Getting manufacturer serialnumber of system harddrive with WMI

我想在我的 C++ 项目中使用 WMI 获取我的系统硬盘驱动器的制造商序列号。

我已经可以从 Win32_DiskDrive Class 查询序列号了。 但是,这returns四个序列号是因为我有四个硬盘。我只想知道我的系统盘的硬盘序列号,但我无法格式化正确的查询。

有人以前做过这个并且知道如何构建这个查询吗?

要获得您的系统 HDD,首先您必须找出哪个硬盘 磁盘 存放您的系统 分区.


使用 Win32_DiskPartition to retrieve the DiskIndex Property and use it to query Win32_DiskDriveIndex 属性。

两者都是Uint32.

This SO Answer 描述了一种检索与卷关联的磁盘驱动器的方法。

另外 "How can I correlate logical drives and physical Drives" on TechNet 可能会有帮助。

首先你必须弄清楚你的 SystemDrive 是什么,然后查询其他 WMI table 以获得 serialNumber。 WMI 在许多 table 中保留序列号,您必须就哪个 table 可以满足您的用例做出更好的选择。如果 serialNumber 是您唯一的用例,那么查询 Win32_LogicalDisk,如果不是,请写评论,我们可以从那里开始工作:)

Select SystemDrive  from Win32_OperatingSystem

Select VolumeSerialNumber  FROM Win32_LogicalDisk where (DriveType = '2' or DriveType = '3') AND deviceid= '<SystemDrive>'

已更新

Select  Index from Win32_DiskPartition where BootPartition ='TRUE'
Select  SerialNumber from Win32_DiskDrive where index='<Index>'

注意:干运行几台机器。

1.GetSystemDirectory() get system Partition.For example, C:\Windows\System32 and C: is System partition.

2.Get 来自 Win32_DiskPartition

的系统分区的磁盘索引

3.Get 来自 Win32_DiskDrive 的 SerialNumber,其中 DiskIndex 是步骤 2