Click here to Skip to main content
Languages » C++ / CLI » General     Intermediate License: The Code Project Open License (CPOL)

Get Physical HDD Serial Number without WMI

By dmihailescu

Retrieve the physical Hard drive ID and other info using low level APIs like DeviceIOControl
C++ (VC8.0), C++/CLI, C#2.0, Windows, .NET (.NET2.0)VS2005, Dev
Revision:2 (See All)
Posted:29 Dec 2006
Updated:6 May 2008
Views:163,912
Bookmarked:92 times
22 votes for this article.
Popularity: 5.96 Rating: 4.44 out of 5
2 votes, 9.1%
1
1 vote, 4.5%
2

3
1 vote, 4.5%
4
18 votes, 81.8%
5

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


Member
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.
Occupation: Web Developer
Location: United States United States

 Msgs 1 to 25 of 122 (Total in Forum: 122) (Refresh)FirstPrevNext
GeneralGreat Example!memberSlowpokee528012:12 3 Dec '09  
Thanks for the code examples!!! Love your avatar! "Drop the catnip and spread em"

The grass IS greener on the other side!

QuestionGreatememberkhaniya 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: Greatememberdmihailescu13:52 18 Nov '09  
I've built it with VS 2005.
Unless you've rebuilt it with VC++ 2008, you'll need c++ 2005 redistributable instead of 2008.
QuestionHDD manufacturer serial no without admin rights for web applications?memberchristopherangler0:17 9 Oct '09  
when Administrator rights is not given, it is not working for web application in windows server 2003 and vista. So is there any other method to get the HDD manufacturer serial no without admin rights?
QuestionDriveInfoEx.dll supports Windows Server 2008?memberchristopherangler18:14 7 Oct '09  
Whether DriveInfoEx.dll supports Windows Server 2008?

Regards,
Christopher
AnswerRe: DriveInfoEx.dll supports Windows Server 2008?memberdmihailescu13:42 8 Oct '09  
I don't know.Frown I haven't tested on anyting but Windows XP.
QuestionDriveInfoEx.dll supports Microsoft Win 64 bit machines?memberchristopherangler18:13 7 Oct '09  
Whether DriveInfoEx.dll supports Microsoft Win 64 bit machines?

Regards,
Christopher
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines?memberdmihailescu13:44 8 Oct '09  
I don't know.Frown I haven't tested on anyting but Windows XP 32 bit.
If you change the include and lib paths to 64 bits file in the cpp project, I assume it might work.
QuestionRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines?memberrazvar3:10 15 Oct '09  
It doesn't work on xp x64, DeviceIoControl fails on DFP_GET_VERSION request.
Any ideas why?

Thank you
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines?memberdmihailescu13:52 15 Oct '09  
are you sure you use the 64 bit include files and 64 bit libs when compiling (under options I think)?
GeneralRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines?memberrazvar22:04 15 Oct '09  
Yes, compile it with 64 bit. The thing is i've done some tests with in vmware workstation and basically it will not get the drive info if the hdd is scsi, I think it has to do with vmware hardware emulation stuff, and i'm sure of that because I've done non vmware test in xp,vista, win 7 (with and without admin rights) and everything works as expected.

Thanks again for the article and your continuous support Smile
QuestionInterface types compatibility with DriveInfoEx.dllmemberchristopherangler18:09 7 Oct '09  
Please provide the details of Hard Disk Interface types compatibility with DriveInfoEx.dll

Regards,
Christopher
AnswerRe: Interface types compatibility with DriveInfoEx.dllmemberdmihailescu13:45 8 Oct '09  
I've tested only on IDE.
QuestionHard Disk Manufacturers compatibiltymemberchristopherangler18:05 7 Oct '09  
Please provide the list of Hard Disk Manufacturers compatibilty with DriveInfoEx.dll

Regards,
Christopher
AnswerRe: Hard Disk Manufacturers compatibiltymemberdmihailescu13:48 8 Oct '09  
I've tested only on WD and Maxtor.
QuestionAdministrator rights for IIS in windows server 2003 & vista business editionmemberchristopherangler1:38 6 Oct '09  
How to set administrator rights for IIS (web application asp.net 2.0) which had the reference of DriveInfoEx.dll to run in windows server 2003 and windows vista business edition ?

Kindly give me inputs.

Regards,
Christopher.
GeneralSame for VS2008...membersoftwaretry1:42 22 Sep '09  
Hello sir,

Will you please update this for VS2008?

So, can able to use it with Visual Studio 2008... Or please provide help to use it with VS2008. It shows so many compilation errors with VS2008...

Thanks!!!
GeneralRequires Visual C++ 2008 runtimesmemberPeter Souza4:26 1 Sep '09  
Any way we can remove this requirement:

http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en[^]
GeneralRe: Requires Visual C++ 2008 runtimesmemberdmihailescu14:18 1 Sep '09  
you can try to find dependencies with depends.exe and see if it works when added manually.
There is no guarantee that it would work that way though.Confused
GeneralGreat work, thanks!memberPeter Souza9:47 26 Aug '09  
This is exactly what I needed -- hard drive serial number without using WMI. Good work!
Generalerror loading driveinfoex.dllmembermano_meee23:58 17 Aug '09  
i'm getting this error when users run my exe that refrenced with driveinfoEx.dll

Could not load file or assembly 'DriveInfoEx, Version=1.1.3487.25050, Culture=neutral, PublicKeyToken=null' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)

but it works great on my machine,could you help me plz.
GeneralRe: error loading driveinfoex.dllmemberdmihailescu14:55 18 Aug '09  
you might be missing .net 2.0 on that machine.
GeneralRe: error loading driveinfoex.dllmemberPeter Souza4:59 1 Sep '09  
This means that you do not have the Visual C++ 2008 Redistributable Package installed.
Generalit worked on vista for me.memberWendel Renner5:01 18 Jun '09  
It worked on vista for me.
Questionvista problemmemberMember 148265322:10 29 May '09  
Do you test it in vista?
i test it for vista but don't work?
Sleepy Confused

CaptainMassoud

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 May 2008
Editor: Deeksha Shenoy
Copyright 2006 by dmihailescu
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project