Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#

Get Physical HDD Serial Number without WMI

Rate me:
Please Sign up or sign in to vote.
4.79/5 (63 votes)
6 May 2008CPOL2 min read 957.9K   62.7K   178   196
Retrieve the physical Hard drive ID and other info using low level APIs like DeviceIOControl

Sample Image - DriveInfo.png

Introduction

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.

Background

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:

C#
m_list = new DriveListEx();
m_list.Load();
//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.

C++
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;
       break;
    }
  }
   if(bAdd)
   {
      WORD* diskdata = new WORD[256];
      ::memcpy(diskdata,pIdSector,256*sizeof(WORD));
      m_list.push_back(diskdata);
   }
  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 release.zip archive, since they are 32 bit for Windows XP.

History

  • 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.

License

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


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

Comments and Discussions

 
Generaldriveinfoex.dll requires administrator rights in win 7 - how to solve it Pin
nileshrajkot22-Apr-11 20:38
nileshrajkot22-Apr-11 20:38 
GeneralMy vote of 4 Pin
nileshrajkot22-Apr-11 20:34
nileshrajkot22-Apr-11 20:34 
GeneralVery nice............It has get me out of My very hard problem....... Pin
Pritesh Aryan15-Apr-11 2:30
Pritesh Aryan15-Apr-11 2:30 
GeneralSorry, i'm a noob Pin
blthbllala18-Jan-11 11:17
blthbllala18-Jan-11 11:17 
GeneralMy vote of 2 Pin
Member 164379218-Dec-10 5:01
professionalMember 164379218-Dec-10 5:01 
GeneralRe: My vote of 2 Pin
dmihailescu29-Nov-11 16:21
dmihailescu29-Nov-11 16:21 
GeneralSerial ID RAID Pin
big_buka12-Dec-10 21:13
big_buka12-Dec-10 21:13 
Generalworks with Win7 x86 not working with xp Pin
InvXeesh11-Nov-10 1:46
InvXeesh11-Nov-10 1:46 
GeneralRe: works with Win7 x86 not working with xp Pin
nethsu28-Nov-10 21:54
nethsu28-Nov-10 21:54 
I'm also having the same problem..Works fine with machines that has C++ runtime library is installed..but what happens if not installed ?? Does any one has a solution for this..pls..
GeneralREAL Hard disk serial number Pin
hingman3-Nov-10 20:50
hingman3-Nov-10 20:50 
GeneralRe: REAL Hard disk serial number Pin
Sabrina Hope11-Jan-11 3:54
Sabrina Hope11-Jan-11 3:54 
GeneralRe: REAL Hard disk serial number Pin
hingman11-Jan-11 16:38
hingman11-Jan-11 16:38 
GeneralMy vote of 5 Pin
Archit937328444829-Oct-10 19:33
Archit937328444829-Oct-10 19:33 
GeneralUnable to run on 64 bit Pin
ilke ALTINPULLUK26-Oct-10 4:12
ilke ALTINPULLUK26-Oct-10 4:12 
Questioncan we write this with c sharp Pin
nethsu24-Oct-10 23:47
nethsu24-Oct-10 23:47 
AnswerRe: can we write this with c sharp Pin
dmihailescu26-Oct-10 16:23
dmihailescu26-Oct-10 16:23 
GeneralRe: can we write this with c sharp Pin
nethsu26-Oct-10 17:06
nethsu26-Oct-10 17:06 
GeneralNot Work with Framework 4.0 Pin
kalpesh280428-Sep-10 22:46
kalpesh280428-Sep-10 22:46 
GeneralSerial No. Almost same as WMI but slightly different. Pin
jawahar_d24-Aug-10 2:32
jawahar_d24-Aug-10 2:32 
QuestionHdd Serial Can Change ? Pin
gsuresh2u27-Jun-10 21:05
gsuresh2u27-Jun-10 21:05 
AnswerRe: Hdd Serial Can Change ? Pin
dmihailescu28-Jun-10 13:09
dmihailescu28-Jun-10 13:09 
GeneralRe: Hdd Serial Can Change ? Pin
Member 381005013-Nov-10 10:37
Member 381005013-Nov-10 10:37 
Generalwebservice can't get Physical HDD list Pin
javitgal22-Jun-10 6:23
javitgal22-Jun-10 6:23 
GeneralRe: webservice can't get Physical HDD list Pin
dmihailescu22-Jun-10 12:53
dmihailescu22-Jun-10 12:53 
GeneralIOEx and DriveListEx could not be found Pin
tmmomdy21-Jun-10 17:08
tmmomdy21-Jun-10 17:08 

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.