Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNot Works on Vista or Win7
Hossam™ Ahmed
15:50 26 Feb '10  
Thanks for your library.. it works on XP with me Smile but not vista or 7.

can u tell me the steps in details to build the solution with for vista.

thanks.
GeneralVS2008 building errors
jp chow
15:43 22 Feb '10  
I've tried to build your source under VS2008 but i get lots of build errors with the DriveInfoEx.dll.

Many 'struct' type redefinition errors.
GeneralGreat Example!
Slowpokee5280
12: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!

QuestionGreate
khaniya suni
2: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
dmihailescu
13: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?
christopherangler
0: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?
christopherangler
18:14 7 Oct '09  
Whether DriveInfoEx.dll supports Windows Server 2008?

Regards,
Christopher
AnswerRe: DriveInfoEx.dll supports Windows Server 2008?
dmihailescu
13: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?
christopherangler
18:13 7 Oct '09  
Whether DriveInfoEx.dll supports Microsoft Win 64 bit machines?

Regards,
Christopher
AnswerRe: DriveInfoEx.dll supports Microsoft Win 64 bit machines?
dmihailescu
13: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?
razvar
3: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?
dmihailescu
13: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?
razvar
22: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.dll
christopherangler
18: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.dll
dmihailescu
13:45 8 Oct '09  
I've tested only on IDE.
QuestionHard Disk Manufacturers compatibilty
christopherangler
18:05 7 Oct '09  
Please provide the list of Hard Disk Manufacturers compatibilty with DriveInfoEx.dll

Regards,
Christopher
AnswerRe: Hard Disk Manufacturers compatibilty
dmihailescu
13:48 8 Oct '09  
I've tested only on WD and Maxtor.
QuestionAdministrator rights for IIS in windows server 2003 & vista business edition
christopherangler
1: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...
softwaretry
1: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 runtimes
Peter Souza
4: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 runtimes
dmihailescu
14: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!
Peter Souza
9:47 26 Aug '09  
This is exactly what I needed -- hard drive serial number without using WMI. Good work!
Generalerror loading driveinfoex.dll
mano_meee
23: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.dll
dmihailescu
14:55 18 Aug '09  
you might be missing .net 2.0 on that machine.
GeneralRe: error loading driveinfoex.dll
Peter Souza
4:59 1 Sep '09  
This means that you do not have the Visual C++ 2008 Redistributable Package installed.


Last Updated 6 May 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010