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

BugNot runing in VS2008 Pin
Menon Santosh13-Mar-13 3:28
professionalMenon Santosh13-Mar-13 3:28 
GeneralRe: Not runing in VS2008 Pin
dmihailescu23-Oct-13 16:39
Memberdmihailescu23-Oct-13 16:39 
Questioni want to get hd Info of other computer through network Pin
ehtshamkh15-Jan-13 2:20
Memberehtshamkh15-Jan-13 2:20 
QuestionIf there are more than 1 HDD? Pin
leomifare21-Oct-12 22:20
Memberleomifare21-Oct-12 22:20 
Questionmac add, ip add, motherboard serial no, processor, local drive Pin
Praveen S22-Jul-12 21:38
MemberPraveen S22-Jul-12 21:38 
GeneralMy vote of 5 Pin
Nox PasNox11-May-12 5:43
MemberNox PasNox11-May-12 5:43 
GeneralMy vote of 5 Pin
Amir Dashti16-Feb-12 5:08
professionalAmir Dashti16-Feb-12 5:08 
QuestionHow to get HDD Device Driver without WMI? Pin
Holt24517-Dec-11 16:29
MemberHolt24517-Dec-11 16:29 
Nice Code, but I try to figure out how to identify the device driver used for a Physical Disk. Could be PCIIDE, MSAHCI, IASTOR, AMD_AHCI or other, depending on the controller and the mode it is using (IDE, AHCI oder RAID). Does anyone have a solution?
Question[My vote of 1] Code from WinSim Inc. Pin
_DiZ29-Nov-11 7:45
Member_DiZ29-Nov-11 7:45 
AnswerRe: [My vote of 1] Code from WinSim Inc. Pin
dmihailescu29-Nov-11 17:14
Memberdmihailescu29-Nov-11 17:14 
GeneralMy vote of 2 Pin
seyyed hamed monem14-Nov-11 2:18
professionalseyyed hamed monem14-Nov-11 2:18 
GeneralRe: My vote of 2 Pin
dmihailescu29-Nov-11 17:19
Memberdmihailescu29-Nov-11 17:19 
QuestionDrive letter Pin
edy_3dz9-Aug-11 11:04
Memberedy_3dz9-Aug-11 11:04 
Generaldriveinfoex.dll requires administrator rights in win 7 - how to solve it Pin
nileshrajkot22-Apr-11 21:38
Membernileshrajkot22-Apr-11 21:38 
GeneralMy vote of 4 Pin
nileshrajkot22-Apr-11 21:34
Membernileshrajkot22-Apr-11 21:34 
GeneralVery nice............It has get me out of My very hard problem....... Pin
Pritesh Aryan15-Apr-11 3:30
MemberPritesh Aryan15-Apr-11 3:30 
GeneralSorry, i'm a noob Pin
blthbllala18-Jan-11 12:17
Memberblthbllala18-Jan-11 12:17 
GeneralMy vote of 2 Pin
Member 164379218-Dec-10 6:01
professionalMember 164379218-Dec-10 6:01 
GeneralRe: My vote of 2 Pin
dmihailescu29-Nov-11 17:21
Memberdmihailescu29-Nov-11 17:21 
GeneralSerial ID RAID Pin
big_buka12-Dec-10 22:13
Memberbig_buka12-Dec-10 22:13 
Generalworks with Win7 x86 not working with xp Pin
InvXeesh11-Nov-10 2:46
MemberInvXeesh11-Nov-10 2:46 
GeneralRe: works with Win7 x86 not working with xp Pin
nethsu28-Nov-10 22:54
Membernethsu28-Nov-10 22:54 
GeneralREAL Hard disk serial number Pin
hingman3-Nov-10 21:50
Memberhingman3-Nov-10 21:50 
GeneralRe: REAL Hard disk serial number Pin
Sabrina Hope11-Jan-11 4:54
MemberSabrina Hope11-Jan-11 4:54 
GeneralRe: REAL Hard disk serial number Pin
hingman11-Jan-11 17:38
Memberhingman11-Jan-11 17:38 

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