Click here to Skip to main content
Click here to Skip to main content

Get Physical HDD Serial Number without WMI

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

 
QuestionGetting Error: Could not load file or assembly 'DriveInfoEx, Version=1.1.3054.15437, Culture=neutral PinmemberMohiyuddin Khan28-May-14 20:19 
AnswerRe: Getting Error: Could not load file or assembly 'DriveInfoEx, Version=1.1.3054.15437, Culture=neutral Pinmemberdmihailescu1-Jul-14 4:44 
QuestionCan not run This Code In VS2010 PinmemberYonos allahyari19-Dec-13 9:31 
AnswerRe: Can not run This Code In VS2010 PinmemberAlbert Holguin19-Dec-13 10:16 
GeneralMessage Removed PinmemberYonos allahyari19-Dec-13 10:21 
AnswerRe: Can not run This Code In VS2010 PinmemberAlbert Holguin19-Dec-13 10:32 
QuestionCan i get usb drive serial no from this code Pinmembersandeepraj331-Dec-13 22:51 
QuestionDriveInfoEx.dll not working for x64 bit computer Pinmemberkalaivanan from Bangalore, India22-Oct-13 4:57 
AnswerRe: DriveInfoEx.dll not working for x64 bit computer Pinmemberdmihailescu23-Oct-13 16:30 
Suggestioni tried ur demo project Pinmemberabhinav_8930-Aug-13 8:22 
GeneralRe: i tried ur demo project Pinmemberdmihailescu1-Jul-14 4:46 
QuestionHow to execute this code PinmemberMember 101775611-Aug-13 11:40 
BugNot runing in VS2008 PinmemberMenon Santosh13-Mar-13 3:28 
GeneralRe: Not runing in VS2008 Pinmemberdmihailescu23-Oct-13 16:39 
Questioni want to get hd Info of other computer through network Pinmemberehtshamkh15-Jan-13 2:20 
QuestionIf there are more than 1 HDD? Pinmemberleomifare21-Oct-12 22:20 
Questionmac add, ip add, motherboard serial no, processor s.no, local drive s.no.... PinmemberPraveen S22-Jul-12 21:38 
GeneralMy vote of 5 PinmemberNox PasNox11-May-12 5:43 
Answeranother method Pinmembery_sir10-May-12 0:06 
GeneralMy vote of 5 PinmemberAmir Dashti16-Feb-12 5:08 
QuestionHow to get HDD Device Driver without WMI? PinmemberHolt24517-Dec-11 16:29 
Question[My vote of 1] Code from WinSim Inc. Pinmember_DiZ29-Nov-11 7:45 
AnswerRe: [My vote of 1] Code from WinSim Inc. Pinmemberdmihailescu29-Nov-11 17:14 
GeneralMy vote of 2 Pinmemberzomorrod.company14-Nov-11 2:18 
GeneralRe: My vote of 2 Pinmemberdmihailescu29-Nov-11 17:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 6 May 2008
Article Copyright 2006 by dmihailescu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid