Click here to Skip to main content
15,879,348 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.4K   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

 
GeneralRe: it will be nice to add also the volume number Pin
dmihailescu24-Jul-07 9:01
dmihailescu24-Jul-07 9:01 
GeneralRe: it will be nice to add also the volume number Pin
albgen24-Jul-07 9:17
albgen24-Jul-07 9:17 
QuestionCould anyone please port this code to use in C++ ??? Pin
amach18-May-07 13:24
amach18-May-07 13:24 
AnswerRe: Could anyone please port this code to use in C++ ??? Pin
dmihailescu21-May-07 11:01
dmihailescu21-May-07 11:01 
GeneralOK, thanks! Now it would be great to achieve it for SCSI... [modified] Pin
amach22-May-07 8:21
amach22-May-07 8:21 
NewsRe: OK, thanks! Now it would be great to achieve it for SCSI... Pin
amach25-May-07 7:56
amach25-May-07 7:56 
QuestionRe: OK, thanks! Now it would be great to achieve it for SCSI... Pin
Sir.Costy28-May-07 1:58
Sir.Costy28-May-07 1:58 
AnswerRe: OK, thanks! Now it would be great to achieve it for SCSI... Pin
amach5-Jun-07 10:03
amach5-Jun-07 10:03 
I've added UnmanagedCode.cpp to my project, and #included UnmanagedCode.h in the Unit/Form I wanted to use it. Without any modifications, it needs
#define NATIVE_CODE
before any
#include "UnmanagedCode.h"
but you may get rid of NATIVE_CODE directive at all, of course...

Alexandre

QuestionRe: OK, thanks! Now it would be great to achieve it for SCSI... Pin
J_E_D_I19-Nov-07 1:58
J_E_D_I19-Nov-07 1:58 
QuestionIs there any way to do it without admin rights? Pin
razvar12-Mar-07 2:28
razvar12-Mar-07 2:28 
AnswerRe: Is there any way to do it without admin rights? Pin
madhan.ponnusamy16-Jul-07 2:15
madhan.ponnusamy16-Jul-07 2:15 
GeneralRe: Is there any way to do it without admin rights? Pin
dmihailescu17-Jul-07 4:34
dmihailescu17-Jul-07 4:34 
GeneralRe: Is there any way to do it without admin rights? Pin
madhan.ponnusamy17-Jul-07 20:47
madhan.ponnusamy17-Jul-07 20:47 
GeneralRe: Is there any way to do it without admin rights? Pin
dmihailescu18-Jul-07 12:55
dmihailescu18-Jul-07 12:55 
GeneralRe: Is there any way to do it without admin rights? Pin
memphis2330-Oct-08 2:18
memphis2330-Oct-08 2:18 
GeneralNumher of Hard disks Pin
Member 38930656-Mar-07 22:31
Member 38930656-Mar-07 22:31 
GeneralRe: Numher of Hard disks Pin
dmihailescu7-Mar-07 13:16
dmihailescu7-Mar-07 13:16 
GeneralDetermine Boot HD Pin
mcanti5-Jan-07 22:24
mcanti5-Jan-07 22:24 
GeneralVMware with two SCSI harddrives Pin
HStrix2-Jan-07 7:46
HStrix2-Jan-07 7:46 
GeneralVMware with three IDE harddrives Pin
HStrix2-Jan-07 6:41
HStrix2-Jan-07 6:41 
GeneralRe: VMware with three IDE harddrives Pin
dmihailescu2-Jan-07 16:09
dmihailescu2-Jan-07 16:09 
GeneralRe: VMware with three IDE harddrives Pin
HStrix2-Jan-07 19:38
HStrix2-Jan-07 19:38 
GeneralRe: VMware with three IDE harddrives Pin
HStrix5-Jan-07 22:48
HStrix5-Jan-07 22:48 
GeneralRe: VMware with three IDE harddrives Pin
memphis2330-Oct-08 2:25
memphis2330-Oct-08 2:25 
QuestionSCSI Arrays Pin
DBuckner29-Dec-06 18:13
DBuckner29-Dec-06 18:13 

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.