5,660,782 members and growing! (15,667 online)
Email Password   helpLost your password?
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++/CLI, C# 2.0, C++, C#, VC8.0, Windows, .NET 2.0, .NETVisual Studio, VS2005, Dev

Posted: 29 Dec 2006
Updated: 6 May 2008
Views: 93,227
Bookmarked: 59 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.47 Rating: 4.44 out of 5
1 vote, 5.9%
1
1 vote, 5.9%
2
0 votes, 0.0%
3
1 vote, 5.9%
4
14 votes, 82.4%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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


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

Other popular C++ / CLI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 69 (Total in Forum: 69) (Refresh)FirstPrevNext
GeneralRetrieve serial number of the disk that contains a specific volumemembermemphis233:40 30 Oct '08  
GeneralRe: Retrieve serial number of the disk that contains a specific volumememberdmihailescu14:06 30 Oct '08  
GeneralHDD Password Informationmembermlwhitl11:11 24 Oct '08  
GeneralRe: HDD Password Informationmemberdmihailescu14:04 30 Oct '08  
GeneralDrive Letters associated with Physical IDmemberTiny BB21:40 15 Oct '08  
GeneralRe: Drive Letters associated with Physical IDmemberdmihailescu13:54 16 Oct '08  
GeneralHDD serial number?memberhongthai19736:33 13 Oct '08  
GeneralRe: HDD serial number?memberdmihailescu9:14 13 Oct '08  
GeneralRe: HDD serial number? [modified]memberhongthai197315:38 13 Oct '08  
QuestionHDD Serial Numbermembersasan564:52 10 Oct '08  
AnswerRe: HDD Serial Numbermemberdmihailescu13:43 10 Oct '08  
GeneralDeviceIoControl returns 0memberMember 469462722:22 24 Jun '08  
GeneralDriveInfo with Drive LettermemberFeilong847:03 16 Jun '08  
GeneralWindows Vista Business 32Bit issuememberFrancesco775:23 4 Jun '08  
GeneralRe: Windows Vista Business 32Bit issuememberdmihailescu13:53 4 Jun '08  
GeneralRe: Windows Vista Business 32Bit issuememberPatrick Chang6:00 12 Jul '08  
GeneralRe: Windows Vista Business 32Bit issuememberdmihailescu10:49 12 Jul '08  
Generalerror in DiskInfo::ReadPhysicalDriveInNTUsingSmart (void) [modified]memberocelot8:03 11 May '08  
GeneralRe: error in DiskInfo::ReadPhysicalDriveInNTUsingSmart (void)memberdmihailescu4:48 12 May '08  
QuestionRAID DisksmemberMember 44504375:49 2 May '08  
QuestionRe: RAID Disksmemberlostone23:17 2 May '08  
AnswerRe: RAID Disksmemberdmihailescu18:26 5 May '08  
AnswerRe: RAID Disksmembernetikus4:26 28 Aug '08  
GeneralNot working on Vista Ultimate 64bitmemberpikob4:28 22 Apr '08  
GeneralRe: Not working on Vista Ultimate 64bitmemberdmihailescu5:07 23 Apr '08  

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:
Copyright 2006 by dmihailescu
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project