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

GeneralReadPhysicalDriveInNTWithZeroRights is the right way Pin
fatass26-Feb-09 12:16
memberfatass26-Feb-09 12:16 
GeneralError Loading DriveinfoEx.dll Pin
Vikash Yadav17-Feb-09 17:43
groupVikash Yadav17-Feb-09 17:43 
GeneralRe: Error Loading DriveinfoEx.dll Pin
dmihailescu18-Feb-09 13:42
memberdmihailescu18-Feb-09 13:42 
GeneralRe: Error Loading DriveinfoEx.dll Pin
Cannoneer19-Jun-09 7:42
memberCannoneer19-Jun-09 7:42 
GeneralRe: Error Loading DriveinfoEx.dll Pin
dmihailescu22-Jun-09 2:43
memberdmihailescu22-Jun-09 2:43 
GeneralRe: Error Loading DriveinfoEx.dll Pin
Pete Souza IV1-Sep-09 4:30
professionalPete Souza IV1-Sep-09 4:30 
GeneralRe: Error Loading DriveinfoEx.dll Pin
InvXeesh11-Nov-10 23:06
memberInvXeesh11-Nov-10 23:06 
QuestionSize of DriveInfoEx.dll... Pin
softwaretry6-Feb-09 2:34
membersoftwaretry6-Feb-09 2:34 
AnswerRe: Size of DriveInfoEx.dll... Pin
dmihailescu6-Feb-09 14:06
memberdmihailescu6-Feb-09 14:06 
GeneralRe: Size of DriveInfoEx.dll... Pin
softwaretry16-Feb-09 8:10
membersoftwaretry16-Feb-09 8:10 
GeneralRe: Size of DriveInfoEx.dll... Pin
dmihailescu18-Feb-09 13:44
memberdmihailescu18-Feb-09 13:44 
AnswerRe: Size of DriveInfoEx.dll... Pin
Vikash Yadav16-Feb-09 22:04
groupVikash Yadav16-Feb-09 22:04 
Generalnot able to use the DriveInfoEx.dll in c# Pin
Ravi Tejas23-Jan-09 2:26
memberRavi Tejas23-Jan-09 2:26 
GeneralRe: not able to use the DriveInfoEx.dll in c# Pin
dmihailescu23-Jan-09 13:50
memberdmihailescu23-Jan-09 13:50 
QuestionVisual Studio 2008 shows errors... Pin
softwaretry19-Jan-09 7:46
membersoftwaretry19-Jan-09 7:46 
AnswerRe: Visual Studio 2008 shows errors... Pin
dmihailescu19-Jan-09 9:43
memberdmihailescu19-Jan-09 9:43 
GeneralRe: Visual Studio 2008 shows errors... Pin
DasMullet9-Feb-09 9:22
memberDasMullet9-Feb-09 9:22 
GeneralRe: Visual Studio 2008 shows errors... Pin
Jacky__E10-Aug-09 7:35
memberJacky__E10-Aug-09 7:35 
QuestionProtect DLL Pin
softwaretry12-Jan-09 7:42
membersoftwaretry12-Jan-09 7:42 
AnswerRe: Protect DLL Pin
dmihailescu13-Jan-09 13:59
memberdmihailescu13-Jan-09 13:59 
GeneralDriveInfoEx.dll in Pin
softwaretry21-Dec-08 3:00
membersoftwaretry21-Dec-08 3:00 
GeneralRe: DriveInfoEx.dll in Pin
dmihailescu22-Dec-08 10:49
memberdmihailescu22-Dec-08 10:49 
GeneralWrong method Pin
kilt1-Dec-08 6:15
memberkilt1-Dec-08 6:15 
GeneralRe: Wrong method Pin
dmihailescu22-Dec-08 10:59
memberdmihailescu22-Dec-08 10:59 
GeneralRetrieve serial number of the disk that contains a specific volume Pin
memphis2330-Oct-08 3:40
membermemphis2330-Oct-08 3:40 

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