Click here to Skip to main content
14,241,891 members

Get Physical HDD Serial Number without WMI

Rate this:
4.79 (62 votes)
Please Sign up or sign in to vote.
4.79 (62 votes)
6 May 2008CPOL
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:

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.

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)

Share

About the Author

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

Comments and Discussions

 
AnswerRe: Interface types compatibility with DriveInfoEx.dll Pin
dmihailescu8-Oct-09 12:45
memberdmihailescu8-Oct-09 12:45 
QuestionHard Disk Manufacturers compatibilty Pin
christopherangler7-Oct-09 17:05
memberchristopherangler7-Oct-09 17:05 
AnswerRe: Hard Disk Manufacturers compatibilty Pin
dmihailescu8-Oct-09 12:48
memberdmihailescu8-Oct-09 12:48 
QuestionAdministrator rights for IIS in windows server 2003 & vista business edition Pin
christopherangler6-Oct-09 0:38
memberchristopherangler6-Oct-09 0:38 
GeneralSame for VS2008... Pin
softwaretry22-Sep-09 0:42
membersoftwaretry22-Sep-09 0:42 
GeneralRequires Visual C++ 2008 runtimes Pin
Pete Souza IV1-Sep-09 3:26
professionalPete Souza IV1-Sep-09 3:26 
GeneralRe: Requires Visual C++ 2008 runtimes Pin
dmihailescu1-Sep-09 13:18
memberdmihailescu1-Sep-09 13:18 
GeneralGreat work, thanks! Pin
Pete Souza IV26-Aug-09 8:47
professionalPete Souza IV26-Aug-09 8:47 
This is exactly what I needed -- hard drive serial number without using WMI. Good work!
Generalerror loading driveinfoex.dll Pin
mano_meee17-Aug-09 22:58
membermano_meee17-Aug-09 22:58 
GeneralRe: error loading driveinfoex.dll Pin
dmihailescu18-Aug-09 13:55
memberdmihailescu18-Aug-09 13:55 
GeneralRe: error loading driveinfoex.dll Pin
Pete Souza IV1-Sep-09 3:59
professionalPete Souza IV1-Sep-09 3:59 
GeneralRe: error loading driveinfoex.dll Pin
InvXeesh11-Nov-10 22:03
memberInvXeesh11-Nov-10 22:03 
Generalit worked on vista for me. Pin
Wendel Renner18-Jun-09 4:01
memberWendel Renner18-Jun-09 4:01 
Questionvista problem Pin
Member 148265329-May-09 21:10
memberMember 148265329-May-09 21:10 
AnswerRe: vista problem Pin
razvar15-Oct-09 2:08
memberrazvar15-Oct-09 2:08 
Questiongiving the .dll a strong name Pin
AKopetzky30-Mar-09 14:05
memberAKopetzky30-Mar-09 14:05 
QuestionRe: giving the .dll a strong name Pin
AKopetzky30-Mar-09 14:07
memberAKopetzky30-Mar-09 14:07 
AnswerRe: giving the .dll a strong name Pin
AKopetzky30-Mar-09 22:31
memberAKopetzky30-Mar-09 22:31 
GeneralReadPhysicalDriveInNTWithZeroRights is the right way Pin
fatass26-Feb-09 11:16
memberfatass26-Feb-09 11:16 
GeneralError Loading DriveinfoEx.dll Pin
Vikash Yadav17-Feb-09 16:43
groupVikash Yadav17-Feb-09 16:43 
GeneralRe: Error Loading DriveinfoEx.dll Pin
dmihailescu18-Feb-09 12:42
memberdmihailescu18-Feb-09 12:42 
GeneralRe: Error Loading DriveinfoEx.dll Pin
Cannoneer19-Jun-09 6:42
memberCannoneer19-Jun-09 6:42 
GeneralRe: Error Loading DriveinfoEx.dll Pin
dmihailescu22-Jun-09 1:43
memberdmihailescu22-Jun-09 1:43 
GeneralRe: Error Loading DriveinfoEx.dll Pin
Pete Souza IV1-Sep-09 3:30
professionalPete Souza IV1-Sep-09 3:30 
GeneralRe: Error Loading DriveinfoEx.dll Pin
InvXeesh11-Nov-10 22:06
memberInvXeesh11-Nov-10 22:06 

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.

Article
Posted 29 Dec 2006

Stats

818.6K views
57.1K downloads
178 bookmarked