#pragma hdrstop
#pragma argsused#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include "hdd.h"const WORD IDE_ATAPI_IDENTIFY = 0xA1; // 读取ATAPI设备的命令
const WORD IDE_ATA_IDENTIFY = 0xEC; // 读取ATA设备的命令
const int DISK_PATH_LEN = 128;
const int DISK_INFO_BUF_LEN = 128;void ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{USHORT i;CHAR temp;for (i = 0; i < uscStrSize; i+=2){temp = szString[i];szString[i] = szString[i+1];szString[i+1] = temp;}
}DWORD GetDiskModelNumber(DWORD driver, CHAR *modelNumber, CHAR *sn_buf, CHAR * fw_buf)
{CHAR sFilePath[DISK_PATH_LEN];BOOL result; // results flagDWORD readed; // discard resultsHANDLE hDevice;WORD i;sprintf(sFilePath, "\\\\.\\PHYSICALDRIVE%d", driver);hDevice = CreateFile(sFilePath, // drive to openGENERIC_READ | GENERIC_WRITE, // access to the driveFILE_SHARE_READ | FILE_SHARE_WRITE, //share modeNULL, // default security attributesOPEN_EXISTING, // disposition0, // file attributesNULL // do not copy file attribute);if (hDevice == INVALID_HANDLE_VALUE){fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError());return (DWORD)-1;}GETVERSIONINPARAMS gvopVersionParams;result = DeviceIoControl(hDevice,SMART_GET_VERSION,NULL,0,&gvopVersionParams,sizeof(gvopVersionParams),&readed,NULL);if (!result) //fail{fprintf(stderr, "SMART_GET_VERSION Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return (DWORD)-1;}if(0 == gvopVersionParams.bIDEDeviceMap){return (DWORD)-1;}// IDE or ATAPI IDENTIFY cmdBYTE btIDCmd;SENDCMDINPARAMS inParams;BYTE nDrive =0;btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;// output structureBYTE outParams[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; // + 512 - 1//fill in the input bufferinParams.cBufferSize = 0; //or IDENTIFY_BUFFER_SIZE ?inParams.irDriveRegs.bFeaturesReg = READ_ATTRIBUTES;inParams.irDriveRegs.bSectorCountReg = 1;inParams.irDriveRegs.bSectorNumberReg = 1;inParams.irDriveRegs.bCylLowReg = 0;inParams.irDriveRegs.bCylHighReg = 0;inParams.irDriveRegs.bDriveHeadReg = (nDrive & 1) ? 0xB0 : 0xA0;inParams.irDriveRegs.bCommandReg = btIDCmd;//inParams.bDriveNumber = nDrive;//get the attributesresult = DeviceIoControl(hDevice,SMART_RCV_DRIVE_DATA,&inParams,sizeof(SENDCMDINPARAMS) - 1,outParams,sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,&readed,NULL);if (!result) //fail{fprintf(stderr, "SMART_RCV_DRIVE_DATA Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return (DWORD)-1;}(void)CloseHandle(hDevice);IDINFO *ip = (IDINFO *)((SENDCMDOUTPARAMS*)outParams)->bBuffer ;// get firmware numbermemset(fw_buf, 0, 8);memcpy(fw_buf, ip->sFirmwareRev, 8);fw_buf[8] = '\0';ChangeByteOrder(fw_buf, 8);printf("\n->Firmware: %s", fw_buf);// get Serial numbermemset(sn_buf, 0, 20);memcpy(sn_buf, ip->sSerialNumber, 20);sn_buf[20] = '\0';ChangeByteOrder(sn_buf, 20);printf("\n->Serial: %s", sn_buf);// get model numbermemset(modelNumber, 0, 40);memcpy(modelNumber, ip->sModelNumber, 40);modelNumber[40] = '\0';ChangeByteOrder(modelNumber, 40);printf("\n->Model: %s", modelNumber);return 0;
}int _tmain(int argc, _TCHAR* argv[])
{char model_buf[40+1], sn_buf[20+1], fw_buf[8+1];GetDiskModelNumber(0, model_buf, sn_buf, fw_buf);// printf("Model: %s\nFirmware: %s\nSN: %s", model_buf, fw_buf, sn_buf);getchar();return 0;
}
hdd.h
typedef struct _IDINFO
{USHORT wGenConfig; // WORD 0: 基本信息字USHORT wNumCyls; // WORD 1: 柱面数USHORT wReserved2; // WORD 2: 保留USHORT wNumHeads; // WORD 3: 磁头数USHORT wReserved4; // WORD 4: 保留USHORT wReserved5; // WORD 5: 保留USHORT wNumSectorsPerTrack; // WORD 6: 每磁道扇区数USHORT wVendorUnique[3]; // WORD 7-9: 厂家设定值CHAR sSerialNumber[20]; // WORD 10-19:序列号USHORT wBufferType; // WORD 20: 缓冲类型USHORT wBufferSize; // WORD 21: 缓冲大小USHORT wECCSize; // WORD 22: ECC校验大小CHAR sFirmwareRev[8]; // WORD 23-26: 固件版本CHAR sModelNumber[40]; // WORD 27-46: 内部型号USHORT wMoreVendorUnique; // WORD 47: 厂家设定值USHORT wReserved48; // WORD 48: 保留struct {USHORT reserved1:8;USHORT DMA:1; // 1=支持DMAUSHORT LBA:1; // 1=支持LBAUSHORT DisIORDY:1; // 1=可不使用IORDYUSHORT IORDY:1; // 1=支持IORDYUSHORT SoftReset:1; // 1=需要ATA软启动USHORT Overlap:1; // 1=支持重叠操作USHORT Queue:1; // 1=支持命令队列USHORT InlDMA:1; // 1=支持交叉存取DMA} wCapabilities; // WORD 49: 一般能力USHORT wReserved1; // WORD 50: 保留USHORT wPIOTiming; // WORD 51: PIO时序USHORT wDMATiming; // WORD 52: DMA时序struct {USHORT CHSNumber:1; // 1=WORD 54-58有效USHORT CycleNumber:1; // 1=WORD 64-70有效USHORT UnltraDMA:1; // 1=WORD 88有效USHORT reserved:13;} wFieldValidity; // WORD 53: 后续字段有效性标志USHORT wNumCurCyls; // WORD 54: CHS可寻址的柱面数USHORT wNumCurHeads; // WORD 55: CHS可寻址的磁头数USHORT wNumCurSectorsPerTrack; // WORD 56: CHS可寻址每磁道扇区数USHORT wCurSectorsLow; // WORD 57: CHS可寻址的扇区数低位字USHORT wCurSectorsHigh; // WORD 58: CHS可寻址的扇区数高位字struct {USHORT CurNumber:8; // 当前一次性可读写扇区数USHORT Multi:1; // 1=已选择多扇区读写USHORT reserved1:7;} wMultSectorStuff; // WORD 59: 多扇区读写设定ULONG dwTotalSectors; // WORD 60-61: LBA可寻址的扇区数USHORT wSingleWordDMA; // WORD 62: 单字节DMA支持能力struct {USHORT Mode0:1; // 1=支持模式0 (4.17Mb/s)USHORT Mode1:1; // 1=支持模式1 (13.3Mb/s)USHORT Mode2:1; // 1=支持模式2 (16.7Mb/s)USHORT Reserved1:5;USHORT Mode0Sel:1; // 1=已选择模式0USHORT Mode1Sel:1; // 1=已选择模式1USHORT Mode2Sel:1; // 1=已选择模式2USHORT Reserved2:5;} wMultiWordDMA; // WORD 63: 多字节DMA支持能力struct {USHORT AdvPOIModes:8; // 支持高级POI模式数USHORT reserved:8;} wPIOCapacity; // WORD 64: 高级PIO支持能力USHORT wMinMultiWordDMACycle; // WORD 65: 多字节DMA传输周期的最小值USHORT wRecMultiWordDMACycle; // WORD 66: 多字节DMA传输周期的建议值USHORT wMinPIONoFlowCycle; // WORD 67: 无流控制时PIO传输周期的最小值USHORT wMinPOIFlowCycle; // WORD 68: 有流控制时PIO传输周期的最小值USHORT wReserved69[11]; // WORD 69-79: 保留struct {USHORT Reserved1:1;USHORT ATA1:1; // 1=支持ATA-1USHORT ATA2:1; // 1=支持ATA-2USHORT ATA3:1; // 1=支持ATA-3USHORT ATA4:1; // 1=支持ATA/ATAPI-4USHORT ATA5:1; // 1=支持ATA/ATAPI-5USHORT ATA6:1; // 1=支持ATA/ATAPI-6USHORT ATA7:1; // 1=支持ATA/ATAPI-7USHORT ATA8:1; // 1=支持ATA/ATAPI-8USHORT ATA9:1; // 1=支持ATA/ATAPI-9USHORT ATA10:1; // 1=支持ATA/ATAPI-10USHORT ATA11:1; // 1=支持ATA/ATAPI-11USHORT ATA12:1; // 1=支持ATA/ATAPI-12USHORT ATA13:1; // 1=支持ATA/ATAPI-13USHORT ATA14:1; // 1=支持ATA/ATAPI-14USHORT Reserved2:1;} wMajorVersion; // WORD 80: 主版本USHORT wMinorVersion; // WORD 81: 副版本USHORT wReserved82[6]; // WORD 82-87: 保留struct {USHORT Mode0:1; // 1=支持模式0 (16.7Mb/s)USHORT Mode1:1; // 1=支持模式1 (25Mb/s)USHORT Mode2:1; // 1=支持模式2 (33Mb/s)USHORT Mode3:1; // 1=支持模式3 (44Mb/s)USHORT Mode4:1; // 1=支持模式4 (66Mb/s)USHORT Mode5:1; // 1=支持模式5 (100Mb/s)USHORT Mode6:1; // 1=支持模式6 (133Mb/s)USHORT Mode7:1; // 1=支持模式7 (166Mb/s) ???USHORT Mode0Sel:1; // 1=已选择模式0USHORT Mode1Sel:1; // 1=已选择模式1USHORT Mode2Sel:1; // 1=已选择模式2USHORT Mode3Sel:1; // 1=已选择模式3USHORT Mode4Sel:1; // 1=已选择模式4USHORT Mode5Sel:1; // 1=已选择模式5USHORT Mode6Sel:1; // 1=已选择模式6USHORT Mode7Sel:1; // 1=已选择模式7} wUltraDMA; // WORD 88: Ultra DMA支持能力USHORT wReserved89[167]; // WORD 89-255
} IDINFO, *PIDINFO;