上一回,,一帆風(fēng)給大家講了講如何讀取計算機的MAC地址,這次聊聊怎么獲取硬盤序列號,。硬盤物理序列號是硬盤的出廠序列號,,它是全球都是唯一的,不會隨著系統(tǒng)的安裝,、硬盤的格式化等操作而改變,,跟mac地址一樣都具有唯一性。
1,,第一步:創(chuàng)建設(shè)備對象,,得到設(shè)備句柄,,設(shè)備為硬盤,。 { CString sFilePath; sFilePath.Format("\\\\.\\PHYSICALDRIVE%d", driver); HANDLE hFile=::CreateFile(sFilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); DWORD dwBytesReturned; GETVERSIONINPARAMS gvopVersionParams; DeviceIoControl(hFile, //向設(shè)備對象發(fā)送SMART_GET_VERSION設(shè)備請求,獲取硬盤屬性 SMART_GET_VERSION, NULL, 0, &gvopVersionParams, sizeof(gvopVersionParams), &dwBytesReturned, NULL),; if(gvopVersionParams.bIDEDeviceMap <= 0) return -2; 2,。第二步,發(fā)送SMART_RCV_DRIVE_DATA設(shè)備請求,,獲取硬盤詳細(xì)信息,。 // IDE or ATAPI IDENTIFY cmd int btIDCmd = 0; SENDCMDINPARAMS InParams; int nDrive =0; btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
// 輸出參數(shù) 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; // 對應(yīng)結(jié)構(gòu)IDSECTOR,見頭文件
pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer; for(int i=0; i < 256; i++) dwDiskData[i] = pIDSector[i];
// 取系列號 ZeroMemory(szSerialNumber, sizeof(szSerialNumber)); strcpy(szSerialNumber, ConvertToString(dwDiskData, 10, 19));
// 取模型號 ZeroMemory(szModelNumber, sizeof(szModelNumber)); strcpy(szModelNumber, ConvertToString(dwDiskData, 27, 46));
return 0; }
BOOL __fastcall DoIdentify( HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP, |