Click here to Skip to main content
14,362,338 members

Get Physical HDD Serial Number without WMI

Rate this:
4.79 (63 votes)
Please Sign up or sign in to vote.
4.79 (63 votes)
6 May 2008CPOL
Retrieve the physical Hard drive ID and other info using low level APIs like DeviceIOControl

Sample Image - DriveInfo.png


Many people looking for a schema to protect their work need to get some information that is hardware specific like the Mac Address or some hard drive serial number.


If you tried other solutions like like this one, it probably did not work for you because it's using the WMI services. I was able to find a solution that worked reasonably well here. It made low level calls to the disk using commands sent by the DeviceIoControl API. The code was not very reusable unless you used native C++. Therefore I brushed it a bit and made it look more Object Oriented. Most importantly, I exposed the drive information through a .NET collection.

Using the Code

Since the collection is written in MC++, I've included some Microsoft DLLs from the redistributable pack in the demo zip. Also it's mandatory to use .NET 2.0 since the collection is generic.

The code is very easy to use from any .NET language, like C# for instance:

m_list = new DriveListEx();
//bind to a a grid view
m_dataGridView.DataSource = m_list;

Points of Interest

The information about the internal drives is gathered in DiskInfo::LoadDiskInfo();

DiskInfo is a native singleton class that wraps the calls to ReadPhysicalDriveInNTWithAdminRights() and ReadIdeDriveAsScsiDriveInNT(). I ignored the ReadPhysicalDriveInNTWithZeroRights() that did not seem to work anyways.

Both functions will call AddIfNew() if they can retrieve the information.

Internally there is a list that holds the raw information about the drives and that is updated when a new drive information was found.

BOOL DiskInfo::AddIfNew(USHORT *pIdSector)
  BOOL bAdd = TRUE;
  for(UINT i =0; i< m_list.size();i++)
    if(memcmp(pIdSector,m_list[i],256 * sizeof(WORD)) == 0)
       bAdd = false;
      WORD* diskdata = new WORD[256];
  return bAdd;

If you are stuck with a non .NET compiler, you could still use the source code from UnmanagedCode.cpp, just uncomment the #define NATIVE_CODE line.
This build is for Windows XP 32 bit systems. If you need it for Vista or 64 bit systems, you should select the right include and lib folders when building and should not use the additional DLLs from the archive, since they are 32 bit for Windows XP.


  • Version 1.1: Added ReadPhysicalDriveInNTUsingSmart for reading the HDD info.
    P.S. I did not get a chance to test it for Windows 95 and alike.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Comments and Discussions

GeneralRe: Retrieve serial number of the disk that contains a specific volume Pin
dmihailescu30-Oct-08 14:06
memberdmihailescu30-Oct-08 14:06 
GeneralHDD Password Information Pin
mlwhitl24-Oct-08 11:11
membermlwhitl24-Oct-08 11:11 
GeneralRe: HDD Password Information Pin
dmihailescu30-Oct-08 14:04
memberdmihailescu30-Oct-08 14:04 
GeneralDrive Letters associated with Physical ID Pin
Tiny BB15-Oct-08 21:40
memberTiny BB15-Oct-08 21:40 
GeneralRe: Drive Letters associated with Physical ID Pin
dmihailescu16-Oct-08 13:54
memberdmihailescu16-Oct-08 13:54 
GeneralRe: Drive Letters associated with Physical ID Pin
kilt1-Dec-08 6:18
memberkilt1-Dec-08 6:18 
QuestionHDD serial number? Pin
hongthai197313-Oct-08 6:33
memberhongthai197313-Oct-08 6:33 
AnswerRe: HDD serial number? Pin
dmihailescu13-Oct-08 9:14
memberdmihailescu13-Oct-08 9:14 
GeneralRe: HDD serial number? [modified] Pin
hongthai197313-Oct-08 15:38
memberhongthai197313-Oct-08 15:38 
QuestionHDD Serial Number Pin
sasan5610-Oct-08 4:52
membersasan5610-Oct-08 4:52 
AnswerRe: HDD Serial Number Pin
dmihailescu10-Oct-08 13:43
memberdmihailescu10-Oct-08 13:43 
GeneralDeviceIoControl returns 0 Pin
Member 469462724-Jun-08 22:22
memberMember 469462724-Jun-08 22:22 
GeneralDriveInfo with Drive Letter Pin
Feilong8416-Jun-08 7:03
memberFeilong8416-Jun-08 7:03 
GeneralWindows Vista Business 32Bit issue Pin
Francesco774-Jun-08 5:23
memberFrancesco774-Jun-08 5:23 
GeneralRe: Windows Vista Business 32Bit issue Pin
dmihailescu4-Jun-08 13:53
memberdmihailescu4-Jun-08 13:53 
GeneralRe: Windows Vista Business 32Bit issue Pin
Patrick Chang12-Jul-08 6:00
memberPatrick Chang12-Jul-08 6:00 
GeneralRe: Windows Vista Business 32Bit issue Pin
dmihailescu12-Jul-08 10:49
memberdmihailescu12-Jul-08 10:49 
Generalerror in DiskInfo::ReadPhysicalDriveInNTUsingSmart (void) [modified] Pin
ocelot11-May-08 8:03
memberocelot11-May-08 8:03 
GeneralRe: error in DiskInfo::ReadPhysicalDriveInNTUsingSmart (void) Pin
dmihailescu12-May-08 4:48
memberdmihailescu12-May-08 4:48 
QuestionRAID Disks Pin
lostone2-May-08 5:49
memberlostone2-May-08 5:49 
QuestionRe: RAID Disks Pin
lostone2-May-08 23:17
memberlostone2-May-08 23:17 
AnswerRe: RAID Disks Pin
dmihailescu5-May-08 18:26
memberdmihailescu5-May-08 18:26 
AnswerRe: RAID Disks Pin
TheWizardOfOz28-Aug-08 4:26
memberTheWizardOfOz28-Aug-08 4:26 
GeneralNot working on Vista Ultimate 64bit Pin
pikob22-Apr-08 4:28
memberpikob22-Apr-08 4:28 
GeneralRe: Not working on Vista Ultimate 64bit Pin
dmihailescu23-Apr-08 5:07
memberdmihailescu23-Apr-08 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 29 Dec 2006


178 bookmarked