VC 之获取硬盘序列号

硬盘物理序列号是硬盘的出厂序列号,它是全球都是唯一的,不会随着系统的安装、硬盘的格式化等操作而改变,跟mac地址一样都具有唯一性。

1,第一步:创建设备对象,得到设备句柄,设备为硬盘。
{
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,       //向设备对象发送
SMART_GET_VERSION设备请求,获取硬盘属性
                SMART_GET_VERSION,
                NULL,
                0,
                &gvopVersionParams,
                sizeof(gvopVersionParams),
                 &dwBytesReturned, NULL);
 if(gvopVersionParams.bIDEDeviceMap <= 0)    return -2; 
2。第二步,发送SMART_RCV_DRIVE_DATA设备请求,获取硬盘详细信息。
    // IDE or ATAPI IDENTIFY cmd
    int btIDCmd = 0;
    SENDCMDINPARAMS InParams;
    int nDrive =0;
    btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;


    // 输出参数
    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; // 对应结构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,
                            PSENDCMDOUTPARAMS pSCOP,
   

你可能感兴趣的:(MFC)