Click here to Skip to main content
Licence CPOL
First Posted 29 Dec 2006
Views 344,643
Downloads 13,712
Bookmarked 140 times

Get Physical HDD Serial Number without WMI

By dmihailescu | 6 May 2008
Retrieve the physical Hard drive ID and other info using low level APIs like DeviceIOControl
3 votes, 7.1%
1
3 votes, 7.1%
2

3
6 votes, 14.3%
4
30 votes, 71.4%
5
4.83/5 - 42 votes
5 removed
μ 4.38, σa 2.00 [?]

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Not Works on Vista or Win7 Pinmemberautenje4:12 21 Jun '10  
GeneralRe: Not Works on Vista or Win7 PinmemberHossam™ Ahmed23:02 27 Jun '10  
GeneralRe: Not Works on Vista or Win7 Pinmemberminregol3:10 27 Jul '10  
GeneralVS2008 building errors Pinmemberjp chow15:43 22 Feb '10  
GeneralRe: VS2008 building errors Pinmembermiliu16:28 9 May '10  
GeneralGreat Example! PinmemberSlowpokee528012:12 3 Dec '09  
QuestionGreate Pinmemberkhaniya suni2:44 18 Nov '09  
Hi
Thanks for this
 
but I an having issue while installing on computer where .Net is not installed.
 

I have installed following
2.0 Framwork is installed
c++ 2008 redistributed package is also installed
 
is there anything missed ??
 
Thanks in advance
 
Plz reply
AnswerRe: Greate Pinmemberdmihailescu13:52 18 Nov '09  
QuestionHDD manufacturer serial no without admin rights for web applications? Pinmemberchristopherangler0:17 9 Oct '09  
QuestionDriveInfoEx.dll supports Windows Server 2008? Pinmemberchristopherangler18:14 7 Oct '09  
AnswerRe: DriveInfoEx.dll supports Windows Server 2008? Pinmemberdmihailescu13:42 8 Oct '09  
QuestionDriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberchristopherangler18:13 7 Oct '09  
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberdmihailescu13:44 8 Oct '09  
QuestionRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberrazvar3:10 15 Oct '09  
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberdmihailescu13:52 15 Oct '09  
GeneralRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberrazvar22:04 15 Oct '09  
QuestionRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? PinmemberAnkit___1:22 26 Apr '10  
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines? Pinmemberdmihailescu13:53 26 Apr '10  
QuestionInterface types compatibility with DriveInfoEx.dll Pinmemberchristopherangler18:09 7 Oct '09  
AnswerRe: Interface types compatibility with DriveInfoEx.dll Pinmemberdmihailescu13:45 8 Oct '09  
QuestionHard Disk Manufacturers compatibilty Pinmemberchristopherangler18:05 7 Oct '09  
AnswerRe: Hard Disk Manufacturers compatibilty Pinmemberdmihailescu13:48 8 Oct '09  
QuestionAdministrator rights for IIS in windows server 2003 & vista business edition Pinmemberchristopherangler1:38 6 Oct '09  
GeneralSame for VS2008... Pinmembersoftwaretry1:42 22 Sep '09  
GeneralRequires Visual C++ 2008 runtimes PinmemberPeter Souza4:26 1 Sep '09  

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.

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