Obtain the hard drive serial number with VC

Source: Internet
Author: User
In the last session, Yan Feng told you how to read the MAC address of a computer and how to obtain the hard disk serial number. The physical serial number of the hard disk is the factory serial number of the hard disk. It is globally unique and will not change with system installation, hard disk formatting, or other operations. It is unique like the mac address.

1. Step 1: create a device object and obtain the device handle. The device is a hard disk.
{
CString sFilePath;
SFilePath. Format ("\\\\\ PHYSICALDRIVE % d", driver );
HANDLE hFile =: CreateFile (sFilePath,
GENERIC_READ | GENERIC_WRITE,
File_pai_read | file_pai_write,
NULL, OPEN_EXISTING,
0, NULL );
DWORD dwBytesReturned;
GETVERSIONINPARAMS gvopVersionParams;
DeviceIoControl (hFile, // Send a SMART_GET_VERSION device request to the device object to obtain the hard disk attribute
SMART_GET_VERSION,
NULL,
0,
& GvopVersionParams,
Sizeof (gvopVersionParams ),
& DwBytesReturned, NULL );
If (gvopVersionParams. bIDEDeviceMap <= 0) Return-2;
2. Step 2: Send the SMART_RCV_DRIVE_DATA device request to obtain the disk details.
// IDE or atapi identify cmd
Int btIDCmd = 0;
SENDCMDINPARAMS InParams;
Int nDrive = 0;
BtIDCmd = (gvopVersionParams. bIDEDeviceMap> nDrive & 0x10 )? IDE_ATAPI_IDENTIFY: IDE_ATA_IDENTIFY;

// Output parameters
BYTE btIDOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE-1];

If (DoIdentify (hFile,
& InParams,
(PSENDCMDOUTPARAMS) btIDOutCmd,
(BYTE) btIDCmd,
(BYTE) nDrive, & dwBytesReturned) = FALSE)Return-3;
: CloseHandle (hFile );

DWORD dwDiskData [256];
USHORT * pIDSector; // The IDSECTOR of the corresponding structure. For details, see the header file.

PIDSector = (USHORT *) (SENDCMDOUTPARAMS *) btIDOutCmd)-> bBuffer;
For (int I = 0; I <256; I ++)DwDiskData [I] = pIDSector [I];

// Obtain the serial number
ZeroMemory (szSerialNumber, sizeof (szSerialNumber ));
Strcpy (szSerialNumber, ConvertToString (dwDiskData, 10, 19 ));

// Obtain the model number
ZeroMemory (szModelNumber, sizeof (szModelNumber ));
Strcpy (szModelNumber, ConvertToString (dwDiskData, 27, 46 ));

Return 0;
}

BOOL _ fastcall DoIdentify (HANDLE hPhysicalDriveIOCTL,
PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP,
BYTE btIDCmd,
BYTE btDriveNum,
PDWORD pdwBytesReturned)
{
PSCIP-> cBufferSize = IDENTIFY_BUFFER_SIZE;
PSCIP-> irDriveRegs. bFeaturesReg = 0;
PSCIP-> irDriveRegs. bsectorcounreg= 1;
PSCIP-> irDriveRegs. bSectorNumberReg = 1;
PSCIP-> irDriveRegs. bCylLowReg= 0;
PSCIP-> irDriveRegs. bCylHighReg = 0;

PSCIP-> irDriveRegs. bDriveHeadReg = (btDriveNum & 1 )? 0xB0: 0xA0;
PSCIP-> irDriveRegs. bCommandReg = btIDCmd;
PSCIP-> bDriveNumber = btDriveNum;
PSCIP-> cBufferSize = IDENTIFY_BUFFER_SIZE;

Return DeviceIoControl (HPhysicalDriveIOCTL,
SMART_RCV_DRIVE_DATA,
(LPVOID) pSCIP,
Sizeof (SENDCMDINPARAMS)-1,
(LPVOID) pSCOP,
Sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE-1,
PdwBytesReturned, NULL );
}

Char * _ fastcall ConvertToString (DWORD dwDiskData [256], int nFirstIndex, int nLastIndex)
{
Static char szResBuf [1024];
Char ss [256];
Int nIndex = 0;
Int nPosition = 0;

For (nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex ++)
{
Ss [nPosition] = (char) (dwDiskData [nIndex]/256 );
NPosition ++;

// Get low BYTE for 2nd character
Ss [nPosition] = (char) (dwDiskData [nIndex] % 256 );
NPosition ++;
}

// End the string
Ss [nPosition] = '\ 0 ';

Int I, index = 0;
For (I = 0; I <nPosition; I ++)
{
If (ss [I] = 0 | ss [I] = 32)Continue;
SzResBuf [index] = ss [I];
Index ++;
}
SzResBuf [index] = 0;

Return szResBuf;
}

For details, search for msdn.

The above code is successfully tested on the WinXP sp2 Platform

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.