473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Get the Serial number of the hard disk

Airslash
221 100+
Hello,

I've written a class that resembles a hard drive. I'm already able using the Windows API to get information such as the remaining free bytes, the sectors and clusters,a nd it all works fine.

The last step that I need is to obtain the serial number of the harddrive in question. From what I can understand in the MSDN library you need to take into account that a Volume can span multiple disks etc, but I don't wanne waste time on that right now, I'd just be happy if I can read the serial number of a single disk.

That kept in mind, i've written a function that retrieves all the data I need, i'm just a bit scared about the part for retrieving the serial. I've read somewhere that I need a VOLUME_DISK_EXTENTS structure and use a 20bytes offset and 20 bytes length to read the serial number.

I've come up with this code so far:
Expand|Select|Wrap|Line Numbers
  1. bool BigBrother::IO::Drive::LoadDriveData() const
  2. {
  3.     // Create the return values
  4.     bool ResultOne, ResultTwo, ResultThree;
  5.  
  6.     // Create the UNC compatible root of the drive.
  7.     AnsiString root = AnsiString(mDriveLetter) + ":\\";
  8.  
  9.     // Set all the datamembers to 0 first, so that we have clean
  10.     // values to work with.
  11.     mSectorsPerCluster = 0;
  12.     mBytesPerSector = 0;
  13.     mFreeClusters = 0;
  14.     mTotalClusters = 0;
  15.     mTotalBytes = 0;
  16.     mFreeBytes = 0;
  17.  
  18.     // Use the Windows API to fill the datamembers. Store the result
  19.     // of the API call in a boolean.
  20.     ResultOne = ::GetDiskFreeSpace(root.c_str(), &mSectorsPerCluster, &mBytesPerSector, &mFreeClusters, &mTotalClusters);
  21.     ResultTwo = ::GetDiskFreeSpaceEx(root.c_str(), 0, (_ULARGE_INTEGER *)&mTotalBytes, (_ULARGE_INTEGER *)&mFreeBytes);
  22.  
  23.     // Calculate the AllocationUnitSiwe of the disk.
  24.     mAllocationUnitSize = (mBytesPerSector * mSectorsPerCluster);
  25.  
  26.     // Create the string to point to our device.
  27.     AnsiString DeviceString = "\\\\.\\" + AnsiString(mDriveLetter) + ":";
  28.  
  29.     // Create a handle to our drive, so we can pass this further down in the
  30.     // Windows API to retrieve the windows Serial.
  31.     HANDLE DriveHandle = ::CreateFileA(DeviceString.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |FILE_SHARE_WRITE, OPEN_EXISTING, 0, 0);
  32.  
  33.     // Check if the handle is valid before continuing down this path.
  34.     if(DriveHandle != INVALID_HANDLE_VALUE)
  35.     {
  36.         // Create a COLUME_DISK_EXTENTS structure and read the info about the drive.
  37.         VOLUME_DISK_EXTENTS VolumeInfo;
  38.         long BytesRead;
  39.         ResultThree = ::DeviceIoControl(DriveHandle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &VolumeInfo, sizeof(VolumeInfo), &BytesReturned, 0);
  40.  
  41.         // Copy the serial number to our datamember.
  42.         // We need to offset the extend with 20 and read a length of 20 bytes to get the entire serial.
  43.         memcpy(&mSerial, VolumeInfo->StartingOffset + 0x20, 0x20);
  44.     }
  45.     // Return the logical result of the Windows API calls.
  46.     return (ResultOne && ResultTwo);
  47. }
  48.  
Can someone who has done this before, explain to me if i'm using the VOLUME_DISK_EXTENTS struct properly, or if I'm missing the ball completly?
Mar 10 '10 #1
0 4698

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
15
by: tom | last post by:
Hi, How do I get the serial number of the harddisk in .NET? I want this to be the same number even if the user has reformatted, so I do not want the volume serial number. Thanx, t
79
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current...
1
by: Ahmed Essa | last post by:
hiiiiiiiiiii, i have aproblem in System.management namespace which use to get the serial number of hard disk. my problem is that when i run my application on windows2000 server it cause execption...
14
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)...
9
by: Nebojsa4 | last post by:
Hi. First, sorry on my weak English to all. Qusetion: How to read (in VB) Manufacturer serial number of Hard disk drive? Not volume/serial number of C:, D:, etc. partitons. For reading...
6
by: Paul Bromley | last post by:
Ok - I have given up on trying to find the active IP address for a given PC. For licensing purposes I need to retrive a unique identifier from the PC that the program is installed on. The Hard disk...
7
by: Adele le Roux | last post by:
Hi All, How can I get the hard disk serial number of a remote computer's C:? The drive will NOT be mapped as a network drive. Thanks, Adele
0
by: preethaAjayan | last post by:
Could anybody please help me with a piece of code to get hard disk's serial number using C#, not volume serial number, actual number that manufactures give to hard disks.
6
by: Matthew Connor | last post by:
Hi all! I'm sure many of you rolled your eyes at the subject trying to recall how many times youv'e heard this question. :) But bear with because I THINK my question is at least slightly different...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.