當前位置:編程學習大全網 - 編程語言 - C++如何獲取硬盤的ID?求代碼!

C++如何獲取硬盤的ID?求代碼!

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.bSectorCountReg = 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;

}

int GetID() //主函數

{

//創建設備對象,得到設備句柄,設備為硬盤。

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;

//發送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;}

  • 上一篇:如何把手中200萬變為2萬億?
  • 下一篇:請用猴子,梅花鹿,森林,音樂會,編寫童話
  • copyright 2024編程學習大全網