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

Get Physical HDD Serial Number without WMI

By , 6 May 2008
 

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugNot runing in VS2008 PinmemberMenon Santosh13 Mar '13 - 2:28 
Questioni want to get hd Info of other computer through network Pinmemberehtshamkh15 Jan '13 - 1:20 
QuestionIf there are more than 1 HDD? Pinmemberleomifare21 Oct '12 - 21:20 
Questionmac add, ip add, motherboard serial no, processor s.no, local drive s.no.... PinmemberPraveen S22 Jul '12 - 20:38 
GeneralMy vote of 5 PinmemberNox PasNox11 May '12 - 4:43 
Answeranother method Pinmembery_sir9 May '12 - 23:06 
GeneralMy vote of 5 PinmemberAmir Dashti16 Feb '12 - 4:08 
QuestionHow to get HDD Device Driver without WMI? PinmemberHolt24517 Dec '11 - 15:29 
Question[My vote of 1] Code from WinSim Inc. Pinmember_DiZ29 Nov '11 - 6:45 
GeneralMy vote of 2 Pinmemberzomorrod.company14 Nov '11 - 1:18 
QuestionDrive letter Pinmemberedy_3dz9 Aug '11 - 10:04 
Generaldriveinfoex.dll requires administrator rights in win 7 - how to solve it Pinmembernileshrajkot22 Apr '11 - 20:38 
GeneralMy vote of 4 Pinmembernileshrajkot22 Apr '11 - 20:34 
GeneralVery nice............It has get me out of My very hard problem....... PinmemberPritesh Aryan15 Apr '11 - 2:30 
GeneralSorry, i'm a noob Pinmemberblthbllala18 Jan '11 - 11:17 
GeneralMy vote of 2 PinmemberMember 164379218 Dec '10 - 5:01 
GeneralSerial ID RAID Pinmemberbig_buka12 Dec '10 - 21:13 
Generalworks with Win7 x86 not working with xp PinmemberMember 437972011 Nov '10 - 1:46 
GeneralREAL Hard disk serial number Pinmemberhingman3 Nov '10 - 20:50 
GeneralMy vote of 5 PinmemberArchit937328444829 Oct '10 - 19:33 
GeneralUnable to run on 64 bit Pinmemberilke ALTINPULLUK26 Oct '10 - 4:12 
Questioncan we write this with c sharp Pinmembernethsu24 Oct '10 - 23:47 
GeneralNot Work with Framework 4.0 Pinmemberkalpesh280428 Sep '10 - 22:46 
GeneralSerial No. Almost same as WMI but slightly different. Pinmemberjawahar_d24 Aug '10 - 2:32 
QuestionHdd Serial Can Change ? Pinmembergsuresh2u27 Jun '10 - 21:05 
Generalwebservice can't get Physical HDD list Pinmemberjavitgal22 Jun '10 - 6:23 
GeneralIOEx and DriveListEx could not be found Pinmembertmmomdy21 Jun '10 - 17:08 
General64 bit dll PinmemberJosh1991184 May '10 - 22:09 
Questioninformation of external hard drives ? Pinmembergeorec11 Apr '10 - 12:17 
GeneralNot Works on Vista or Win7 PinmemberHossam™ Ahmed26 Feb '10 - 14:50 
GeneralVS2008 building errors Pinmemberjp chow22 Feb '10 - 14:43 
GeneralGreat Example! PinmemberSlowpokee52803 Dec '09 - 11:12 
QuestionGreate Pinmemberkhaniya suni18 Nov '09 - 1:44 
QuestionHDD manufacturer serial no without admin rights for web applications? Pinmemberchristopherangler8 Oct '09 - 23:17 
QuestionDriveInfoEx.dll supports Windows Server 2008? Pinmemberchristopherangler7 Oct '09 - 17:14 
QuestionDriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberchristopherangler7 Oct '09 - 17:13 
QuestionInterface types compatibility with DriveInfoEx.dll Pinmemberchristopherangler7 Oct '09 - 17:09 
QuestionHard Disk Manufacturers compatibilty Pinmemberchristopherangler7 Oct '09 - 17:05 
QuestionAdministrator rights for IIS in windows server 2003 & vista business edition Pinmemberchristopherangler6 Oct '09 - 0:38 
GeneralSame for VS2008... Pinmembersoftwaretry22 Sep '09 - 0:42 
GeneralRequires Visual C++ 2008 runtimes PinmemberPeter Souza1 Sep '09 - 3:26 
GeneralGreat work, thanks! PinmemberPeter Souza26 Aug '09 - 8:47 
Generalerror loading driveinfoex.dll Pinmembermano_meee17 Aug '09 - 22:58 
Generalit worked on vista for me. PinmemberWendel Renner18 Jun '09 - 4:01 
Questionvista problem PinmemberMember 148265329 May '09 - 21:10 
Questiongiving the .dll a strong name PinmemberMember 472352230 Mar '09 - 14:05 
GeneralReadPhysicalDriveInNTWithZeroRights is the right way Pinmemberfatass26 Feb '09 - 11:16 
GeneralError Loading DriveinfoEx.dll PinmemberVikash Yadav17 Feb '09 - 16:43 
QuestionSize of DriveInfoEx.dll... Pinmembersoftwaretry6 Feb '09 - 1:34 
Generalnot able to use the DriveInfoEx.dll in c# PinmemberRavi Tejas23 Jan '09 - 1:26 

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

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