#define _CRT_SECURE_NO_WARNINGS #include <windows.h> #include <iostream> #include <winioctl.h> #include <string>
const WORD IDE_ATAPI_IDENTIFY = 0xA1; const WORD IDE_ATA_IDENTIFY = 0xEC;
BOOL Trim(char* szStr) { int i = 0, j = 0, iFirst = -1, iLast = -1; int iLen = strlen(szStr); char szTemp[256] = { 0 }; for (i = 0; i < iLen; i++) { if (' ' != szStr[i]) { iFirst = i; break; } } for (i = (iLen - 1); 0 <= i; i--) { if (' ' != szStr[i]) { iLast = i; break; } } if (-1 == iFirst || -1 == iLast) { return FALSE; } for (i = iFirst; i <= iLast; i++) { szTemp[j] = szStr[i]; j++; } szTemp[j] = '\0'; strcpy(szStr, szTemp);
return TRUE; }
char* __fastcall ConvertToString(DWORD dwDiskData[256],int iFirstIndex,int iLastIndex) { static char szResBuf[256]; int iIndex = 0; int iPosition = 0;
for (iIndex = iFirstIndex; iIndex <= iLastIndex; iIndex++) { szResBuf[iPosition] = (char)(dwDiskData[iIndex] / 256); iPosition++; szResBuf[iPosition] = (char)(dwDiskData[iIndex] % 256); iPosition++; } szResBuf[iPosition] = '\0';
Trim(szResBuf); return szResBuf; }
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;
return DeviceIoControl(hPhysicalDriveIOCTL,SMART_RCV_DRIVE_DATA,(LPVOID)pSCIP,sizeof(SENDCMDINPARAMS) - 1, (LPVOID)pSCOP,sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,pdwBytesReturned,NULL); return FALSE; }
int GetDiskInfo(int iDriver, char* szSerialNumber, char* szModelNumber, char* szFirmwareNumber) { char szFilePath[64] = { 0 }; sprintf(szFilePath, "\\\\.\\PHYSICALDRIVE%d", iDriver);
HANDLE hFile = CreateFileA(szFilePath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL); if (INVALID_HANDLE_VALUE == hFile) { return -1; }
DWORD dwBytesReturned = 0; GETVERSIONINPARAMS gvopVersionParam; DeviceIoControl(hFile,SMART_GET_VERSION,NULL,0,&gvopVersionParam,sizeof(gvopVersionParam),&dwBytesReturned,NULL); if (0 >= gvopVersionParam.bIDEDeviceMap) { return -2; }
unsigned int uiIDCmd = 0; SENDCMDINPARAMS InParams; unsigned int uiDrive = 0; uiIDCmd = (gvopVersionParam.bIDEDeviceMap >> uiDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
BYTE btOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; if (FALSE == DoIdentify(hFile,&InParams,(SENDCMDOUTPARAMS*)btOutCmd,(BYTE)uiIDCmd,(BYTE)uiDrive,&dwBytesReturned)) { return -3; }
CloseHandle(hFile);
DWORD dwDiskData[256]; USHORT* pIDSector = NULL; pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btOutCmd)->bBuffer; for (int i = 0; i < 256; i++) { dwDiskData[i] = pIDSector[i]; }
strcpy(szSerialNumber, ConvertToString(dwDiskData, 10, 19));
strcpy(szModelNumber, ConvertToString(dwDiskData, 27, 46));
strcpy(szFirmwareNumber, ConvertToString(dwDiskData, 23, 26));
return 0; }
int main(int argc,char *argv[]) { char SerialNumber[64]; char ModelNumber[64]; char FirmwareNumber[64];
if (0 == GetDiskInfo(0, SerialNumber, ModelNumber, FirmwareNumber)) { std::cout << "序列号: " << SerialNumber << std::endl; std::cout << "硬盘型号: " << ModelNumber << std::endl; std::cout << "固件版本:" << FirmwareNumber << std::endl; }
system("pause"); return 0; }
|