Click here to Skip to main content
14,367,233 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

GeneralSerial No. Almost same as WMI but slightly different. Pin
jawahar_d24-Aug-10 3:32
memberjawahar_d24-Aug-10 3:32 
QuestionHdd Serial Can Change ? Pin
gsuresh2u27-Jun-10 22:05
membergsuresh2u27-Jun-10 22:05 
AnswerRe: Hdd Serial Can Change ? Pin
dmihailescu28-Jun-10 14:09
memberdmihailescu28-Jun-10 14:09 
GeneralRe: Hdd Serial Can Change ? Pin
Member 381005013-Nov-10 11:37
memberMember 381005013-Nov-10 11:37 
Generalwebservice can't get Physical HDD list Pin
javitgal22-Jun-10 7:23
memberjavitgal22-Jun-10 7:23 
GeneralRe: webservice can't get Physical HDD list Pin
dmihailescu22-Jun-10 13:53
memberdmihailescu22-Jun-10 13:53 
GeneralIOEx and DriveListEx could not be found Pin
tmmomdy21-Jun-10 18:08
membertmmomdy21-Jun-10 18:08 
GeneralRe: IOEx and DriveListEx could not be found Pin
dmihailescu22-Jun-10 13:50
memberdmihailescu22-Jun-10 13:50 
General64 bit dll Pin
Josh1991184-May-10 23:09
memberJosh1991184-May-10 23:09 
Questioninformation of external hard drives ? Pin
georec11-Apr-10 13:17
membergeorec11-Apr-10 13:17 
I tried the application under windows xp. It correctly lists the internal drives Smile | :) , but not the external drives Unsure | :~ . Is there a method to get information about external hard drives, e.g. usb drives ?
AnswerRe: information of external hard drives ? Pin
dmihailescu11-Apr-10 17:42
memberdmihailescu11-Apr-10 17:42 
GeneralRe: information of external hard drives ? Pin
georec12-Apr-10 11:04
membergeorec12-Apr-10 11:04 
GeneralNot Works on Vista or Win7 Pin
Hossam™ Ahmed26-Feb-10 15:50
memberHossam™ Ahmed26-Feb-10 15:50 
GeneralRe: Not Works on Vista or Win7 Pin
autenje21-Jun-10 4:12
memberautenje21-Jun-10 4:12 
GeneralRe: Not Works on Vista or Win7 Pin
Hossam™ Ahmed27-Jun-10 23:02
memberHossam™ Ahmed27-Jun-10 23:02 
GeneralRe: Not Works on Vista or Win7 Pin
minregol27-Jul-10 3:10
memberminregol27-Jul-10 3:10 
GeneralVS2008 building errors Pin
jp chow22-Feb-10 15:43
memberjp chow22-Feb-10 15:43 
GeneralRe: VS2008 building errors Pin
miliu9-May-10 16:28
membermiliu9-May-10 16:28 
GeneralGreat Example! Pin
Slowpokee52803-Dec-09 12:12
memberSlowpokee52803-Dec-09 12:12 
QuestionGreate Pin
Khaniya18-Nov-09 2:44
professionalKhaniya18-Nov-09 2:44 
AnswerRe: Greate Pin
dmihailescu18-Nov-09 13:52
memberdmihailescu18-Nov-09 13:52 
QuestionHDD manufacturer serial no without admin rights for web applications? Pin
christopherangler9-Oct-09 0:17
memberchristopherangler9-Oct-09 0:17 
QuestionDriveInfoEx.dll supports Windows Server 2008? Pin
christopherangler7-Oct-09 18:14
memberchristopherangler7-Oct-09 18:14 
AnswerRe: DriveInfoEx.dll supports Windows Server 2008? Pin
dmihailescu8-Oct-09 13:42
memberdmihailescu8-Oct-09 13:42 
QuestionDriveInfoEx.dll supports Microsoft Win 64 bit machines? Pin
christopherangler7-Oct-09 18:13
memberchristopherangler7-Oct-09 18:13 

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