C ++ hardware information to obtain the hard disk serial number and the hard disk serial number

Source: Internet
Author: User

C ++ hardware information to obtain the hard disk serial number and the hard disk serial number

This section describes how to obtain the serial number of a hard disk, not the serial number of a hard disk partition. The core is throughDeviceIoControlInterface to obtain the version and data information of the hard disk.

Simple.


[1] h header file

// If there is a compilation problem, add # include <afxwin. h> # if! Defined (partition _) # define partition _ # if _ MSC_VER> 1000 # pragma once # endif // _ MSC_VER> 1000 # include <iostream> # include <windows. h> # define DFP_GET_VERSION0x00074080 # define DFP_SEND_DRIVE_COMMAND0x0007c084 # define DFP_RECEIVE_DRIVE_DATA0x0007c088 typedef struct _ GETVERSIONOUTPARAMS {BYTE bVer Sion; // Binary driver version. BYTE bRevision; // Binary driver revision. BYTE bReserved; // Not used. BYTE bIDEDeviceMap; // Bit map of IDE devices. DWORD fCapabilities; // Bit mask of driver capabilities. DWORD dwReserved [4]; // For future use .} GETVERSIONOUTPARAMS, * PGETVERSIONOUTPARAMS, * LPGETVERSIONOUTPARAMS; typedef struct _ IDEREGS {BYTE bFeaturesReg; // Used for specifying SMART "command S ". BYTE bsectorcounreg; // IDE sector count register BYTE bSectorNumberReg; // IDE sector number register BYTE bCylLowReg; // IDE low order cylinder value BYTE bCylHighReg; // IDE high order cylinder value BYTE bDriveHeadReg; // IDE drive/head register BYTE bCommandReg; // Actual IDE command. BYTE bReserved; // reserved for future use. must be zero .} IDEREGS, * PIDEREGS, * LPIDEREGS; typedef stru Ct _ SENDCMDINPARAMS {DWORD cBufferSize; // Buffer size in bytes IDEREGS irDriveRegs; // Structure with drive register values. BYTE bDriveNumber; // Physical drive number to send command to (0, 1, 2, 3 ). BYTE bReserved [3]; // Reserved for future expansion. DWORD dwReserved [4]; // For future use .} SENDCMDINPARAMS, * PSENDCMDINPARAMS, * LPSENDCMDINPARAMS; typedef struct _ DRIVERSTATUS {BYTE bDriverError; // Error code from driver, // or 0 if no error. BYTE bIDEStatus; // Contents of IDE Error register. // Only valid when bDriverError // is SMART_IDE_ERROR. BYTE bReserved [2]; // Reserved for future expansion. DWORD dwReserved [2]; // Reserved for future expansion .} DRIVERSTATUS, * PDRIVERSTATUS, * LPDRIVERSTATUS; typedef struct _ SENDCMDOUTPARAMS {DWORD cBufferSize; // Size of bBuffer in bytes DRIV ERSTATUS DriverStatus; // Driver status structure. BYTE bBuffer [512]; // Buffer of arbitrary length // in which to store the data read from the drive .} optional, * optional, * optional; typedef struct _ IDSECTOR {USHORT wGenConfig; USHORT wNumCyls; USHORT wReserved; USHORT wNumHeads; USHORT failed Que [3]; CHAR sSerialNumber [20]; USHORT wBufferType; USHORT wBufferSize; USHORT wECCSize; CHAR sFirmwareRev [8]; CHAR sModelNumber [40]; USHORT limit; USHORT wDoubleWordIO; USHORT wCapabilities; USHORT wReserved1; USHORT wPIOTiming; USHORT wDMATiming; USHORT wBS; USHORT wNumCurrentCyls; USHORT future; ULONG future; SectorStuff; ULONG serial; USHORT wMultiWordDMA; BYTE bReserved [128];} IDSECTOR, * PIDSECTOR; // serial number of the hard disk // -------------------------------------------------------------- BOOL GetHDSerial (char * lpszHD, int lename = 128); # endif //! Defined (afx_hdserial_h1_1f6d2512_eba9_4443_9fef_de78d7a28afb1_included _)

[2] cpp File

# Include "stdafx. h "# include" HDSerial. h "void ChangeByteOrder (PCHAR szString, USHORT uscStrSize) {USHORT I = 0; CHARtemp = '\ 0'; for (I = 0; I <uscStrSize; I + = 2) {temp = szString [I]; szString [I] = szString [I + 1]; szString [I + 1] = temp ;}} // serial // hard drive serial number // -------------------------------------------------------------- BOOL GetHDSerial (char * lpszHD, I Nt len/* = 128 */) {BOOLbRtn = FALSE; random = 0; charszhd [80] = {0}; random; HANDLEhDrive = NULL; GETVERSIONOUTPARAMS vers; SENDCMDINPARAMSin; SENDCMDOUTPARAMSout; zeroMemory (& vers, sizeof (vers); ZeroMemory (& in, sizeof (in); ZeroMemory (& out, sizeof (out); // search for four physical hard disks, take the first physical hard disk with data for (int j = 0; j <4; j ++) {sprintf (szhd ,"\\\\. \ PhysicalDrive % d ", j); hDrive = CreateFileA (szhd, GENERIC_REA D | GENERIC_WRITE, file_cmd_read | file_cmd_write, 0, OPEN_EXISTING,); if (NULL = hDrive) {continue;} if (! DeviceIoControl (hDrive, DFP_GET_VERSION, 0, 0, & vers, sizeof (vers), & bytesRtn, 0) {goto FOREND;} // If IDE identify command not supported, fails if (! (Vers. fCapabilities & 1) {goto FOREND;} // Identify the IDE drives if (j & 1) {in. irDriveRegs. bDriveHeadReg = 0xb0;} else {in. irDriveRegs. bDriveHeadReg = 0xa0;} if (vers. fCapabilities & (16> j) {// We don't detect a ATAPI device. goto FOREND;} else {in. irDriveRegs. bCommandReg = 0xec;} in. bDriveNumber = j; in. irDriveRegs. bsectorcounreg = 1; in. irDriveRegs. bSectorNumberReg = 1; in. cBufferSize = 512; if (! DeviceIoControl (hDrive, DFP_RECEIVE_DRIVE_DATA, & in, sizeof (in), & out, sizeof (out), & bytesRtn, 0) {// "DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA "<endl; goto FOREND;} phdinfo = (PIDSECTOR) out. bBuffer; chars [21] = {0}; memcpy (s, phdinfo-> sSerialNumber, 20); s [20] = 0; ChangeByteOrder (s, 20 ); // Delete the space character int ix = 0; for (ix = 0; ix <20; ix ++) {if (s [ix] = '') {continue ;} break;} memcpy (lpszHD, s + ix, 20); bRtn = TRUE; break; FOREND: CloseHandle (hDrive); hDrive = NULL;} CloseHandle (hDrive ); hDrive = NULL; return (bRtn );}

[3] Test

char lpszMac[128] = {0};GetHDSerial(lpszMac);



C Programming in windows x to obtain the computer hardware serial number (cpu hard disk bios master edition )! Worried! Email wangyu850722 @ 163com

1. Obtain the disk serial number, volume label, and file system type.
The disk serial number is re-generated after each floppy disk or hard disk formatting and is not repeated. Many programmers use this encryption. You can also modify this function to obtain the disk volume label and file system type information.
Statement:
Private Declare Function GetVolumeInformation Lib "kernel32.dll" Alias "volumes" (ByVal lpRootPathName As String, ByVal encoded As String, ByVal nVolumeNameSize As Integer, stored As Long, stored As Long, lpFileSystemFlags As Long, byVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
Code:
Function GetSerialNumber (sRoot As String) As Long
Dim lSerialNum As Long
Dim R As Long
Dim sTemp1 As String, sTemp2 As String
StrLabel = String $ (255, Chr $(0 ))
Note: disk volume label
StrType = String $ (255, Chr $(0 ))
Note: The file system type is generally FAT.
R = GetVolumeInformation (sRoot, strLabel, Len (strLabel), lSerialNum, 0, 0, strType, Len (strType ))
GetSerialNumber = lSerialNum
Note: Mark the disk volume in strLabel.
Note: In strType, It is the file system type.
End Function
Usage:
If the drive does not exist, the function returns 0. If it is a non-root directory, 0 is returned:
LSerial = GetSerialNumber ("c :\")
2. You only need to paste the following code into the VB project and save it as a class. I have tested all the code and used it in many development projects.
Usage:
... The remaining full text>
 
How can I obtain the hardware information of a visitor's machine, such as the hard disk serial number and cpu id?

You can download a drive life that can detect the hardware information of the visitor's machine.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.