473,765 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get the Serial number of the hard disk

Airslash
221 New Member
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_EXT ENTS 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_EXT ENTS struct properly, or if I'm missing the ball completly?
Mar 10 '10 #1
0 4895

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

Similar topics

21
43069
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 (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table. Much appreciated if you can help! Thanks
15
9692
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
14120
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 operating system would be appropriate. However, I do not know how to retrieve the information. Could you help? Klaus
1
2400
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 message called "invalid class", mainly the class called "ManagementObjectSearcher" which retrieve all hard disk information. thanks, ahmed3essa
14
28185
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) Win32_PhysicalMedia (a class) property SerialNumber." I'm sorry to admit it bit I am really undereducated on how to incorporate some of the Windows SDK stuff into VBA apps. Anyone know of some sample code that will allow us to read the C drive hardware...
9
20724
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 volume/serial number of hard disk C: etc, You can use Microsoft Scripting Runtime (in VB):
6
30711
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 serial number would do fine, but on Googling it seems that some of the sample code will now laways work on W2K, and not without administrator rights. Hence my question - how can I consistently get a UNIQUE PC identifier using vb.net 2003 - not...
7
1734
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
1726
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
10489
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 from the typical post. I would like to obtain the manufactuer's embedded serial number (NOT the volume serial that changes with each format) of USB drives. I found some very handy code to obtain a regular SMART-enabled IDE drive's information...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.