简体   繁体   中英

Getting manufacturer serialnumber of system harddrive with WMI

I want to get the manufacturer serialnumber of my system HDD using WMI in my c++ project.

I am already able to query the SerialNumber from the Win32_DiskDrive Class. However, this returns four serial numbers because I have four HDD. I only want to know the HDD serialnumber of my system disk but I am not able to format the right query.

Anyone done this before and knows how to construct this query?

To get your System HDD , first you have to find out which Hard Disk holds your System Partition .


Use Win32_DiskPartition to retrieve the DiskIndex Property and use it to query Win32_DiskDrive with it's Index property.

Both are Uint32 .

This SO Answer describes a way of retrieving DiskDrives associated to volumes.

Also "How can I correlate logical drives and physical Drives" on TechNet may help.

First you have to figure out what is your SystemDrive and then query other WMI table to get the serialNumber . WMI keeps serialnumber in many tables, you have to make a better choice regarding which table can fulfill your use case. If serialNumber is your only use case then query Win32_LogicalDisk , if not, write a comment and we can work from there :)

Select SystemDrive  from Win32_OperatingSystem

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

Updated

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

Note: Give it a dry run for couple of machines.

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

2.Get DiskIndex of System partition from Win32_DiskPartition

3.Get SerialNumber from Win32_DiskDrive which DiskIndex is step 2

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