学习笔记
获取硬盘的smart信息,获取序列号只支持机械硬盘:
平台:win10,vs2010
createfile打开文件权限不够的解决办法: 项目-属性-连接器-清单文件-UAC执行级别,选择requireAdministrator。
头文件:
#define GB(x) (x.HighPart << 2) + (x.LowPart >> 20) / 1024.0
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=支持DMA
USHORT LBA:1; // 1=支持LBA
USHORT DisIORDY:1; // 1=可不使用IORDY
USHORT IORDY:1; // 1=支持IORDY
USHORT 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=已选择模式0
USHORT Mode1Sel:1; // 1=已选择模式1
USHORT Mode2Sel:1; // 1=已选择模式2
USHORT 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-1
USHORT ATA2:1; // 1=支持ATA-2
USHORT ATA3:1; // 1=支持ATA-3
USHORT ATA4:1; // 1=支持ATA/ATAPI-4
USHORT ATA5:1; // 1=支持ATA/ATAPI-5
USHORT ATA6:1; // 1=支持ATA/ATAPI-6
USHORT ATA7:1; // 1=支持ATA/ATAPI-7
USHORT ATA8:1; // 1=支持ATA/ATAPI-8
USHORT ATA9:1; // 1=支持ATA/ATAPI-9
USHORT ATA10:1; // 1=支持ATA/ATAPI-10
USHORT ATA11:1; // 1=支持ATA/ATAPI-11
USHORT ATA12:1; // 1=支持ATA/ATAPI-12
USHORT ATA13:1; // 1=支持ATA/ATAPI-13
USHORT ATA14:1; // 1=支持ATA/ATAPI-14
USHORT 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=已选择模式0
USHORT Mode1Sel:1; // 1=已选择模式1
USHORT Mode2Sel:1; // 1=已选择模式2
USHORT Mode3Sel:1; // 1=已选择模式3
USHORT Mode4Sel:1; // 1=已选择模式4
USHORT Mode5Sel:1; // 1=已选择模式5
USHORT Mode6Sel:1; // 1=已选择模式6
USHORT Mode7Sel:1; // 1=已选择模式7
} wUltraDMA; // WORD 88: Ultra DMA支持能力
USHORT wReserved89[167]; // WORD 89-255
} IDINFO, *PIDINFO;
.cpp文件:
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include "h.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 putDrivesType(const char* lpRootPathName)
{
UINT uDriverType = GetDriveType(lpRootPathName);
switch(uDriverType) {
case DRIVE_UNKNOWN :puts("->未知的磁盘类型"); break;
case DRIVE_NO_ROOT_DIR: puts("->路径无效"); break;
case DRIVE_REMOVABLE: puts("->可移动磁盘"); break;
case DRIVE_FIXED: puts("->固定磁盘"); break;
case DRIVE_REMOTE: puts("->网络磁盘"); break;
case DRIVE_CDROM: puts("->光驱"); break;
case DRIVE_RAMDISK: puts("->内存映射盘"); break;
default:
break;
}
}
void putDrivesFreeSpace(const char* lpRootPathName)
{
//unsigned long long available,total,free;
ULARGE_INTEGER available,total,free; //ULARGE_INTEGER 一个64位的无符号整型值
if(GetDiskFreeSpaceEx(lpRootPathName,(ULARGE_INTEGER*)&available,(ULARGE_INTEGER*)&total,(ULARGE_INTEGER*)&free)){
printf(" | 总容量 = %.2f GB,可用空间 = %.2f GB,空闲空间 = %.2f GB\n",
GB(total),GB(available),GB(free));
}else{
puts("获取容量信息失败");
}
}
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;
}
}
void GetDiskModelNumber(int count,DWORD driver, CHAR *modelNumber, CHAR *sn_buf, CHAR * fw_buf)
{
// CHAR sFilePath[DISK_PATH_LEN] ;
BOOL result; // results flag
DWORD readed; // discard results
HANDLE hDevice;
char sFilePath[255] ;
while(driver!=count){ //循环
sprintf(sFilePath, "\\\\.\\PhysicalDrive%d", driver);
hDevice = CreateFile(
sFilePath, // drive to open
GENERIC_READ | GENERIC_WRITE,// GENERIC_READ|GENERIC_WRITE , // access to the drive
FILE_SHARE_READ| FILE_SHARE_WRITE , //share mode
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL // do not copy file attribute
);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("无法识别序列号等信息");
break;
//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
{
printf("无法识别序列号等信息");
break;
// fprintf(stderr, "SMART_GET_VERSION Error: %ld\n", GetLastError());
// (void)CloseHandle(hDevice);
// return (DWORD)-1;
}
if(0 == gvopVersionParams.bIDEDeviceMap)
{
printf("无法识别序列号等信息");
break;
//return (DWORD)-1;
}
// IDE or ATAPI IDENTIFY cmd
BYTE btIDCmd;
SENDCMDINPARAMS inParams;
BYTE nDrive =0;
btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
// output structure
BYTE outParams[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; // + 512 - 1
//fill in the input buffer
inParams.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 attributes
result = DeviceIoControl(
hDevice,
SMART_RCV_DRIVE_DATA,
&inParams,
sizeof(SENDCMDINPARAMS) - 1,
outParams,
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
&readed,
NULL);
if (!result) //fail
{
printf("无法识别序列号等信息");
break;
//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 number
memset(fw_buf, 0, 8);
memcpy(fw_buf, ip->sFirmwareRev, 8);
fw_buf[8] = '\0';
ChangeByteOrder(fw_buf, 8);
printf("\n->固件: %s", fw_buf);
// get model number
memset(modelNumber, 0, 40);
memcpy(modelNumber, ip->sModelNumber, 40);
modelNumber[40] = '\0';
ChangeByteOrder(modelNumber, 40);
printf("\n->型号: %s", modelNumber);
// get Serial number
memset(sn_buf, 0, 20);
memcpy(sn_buf, ip->sSerialNumber+10, 12);
sn_buf[12] = '\0';
ChangeByteOrder(sn_buf, 12);
printf("\n->序列号: %s", sn_buf);
break;
}
// return 0;
}
void capacity(char* model_buf, char* sn_buf, char* fw_buf)
{
int count = 0;
DWORD dw ;
DWORD dwSize = MAX_PATH;
DWORD driver = 0;
char szLogicalDrives[MAX_PATH] = {0}; //缓冲区
DWORD dwResult = GetLogicalDriveStrings(dwSize,szLogicalDrives); //获取逻辑驱动器号字符串
//获取驱动器数
dw = GetLogicalDrives();
while( dw != 0 )
{
if(dw&1 != 0)
{
count++;
}
dw>>=1;
}
printf("驱动器个数:%d\n",count);
printf("----------------------------\n");
//处理获取到的结果
if (dwResult > 0 && dwResult <= MAX_PATH) {
char* szSingleDrive = szLogicalDrives; //从缓冲区起始地址开始
while(*szSingleDrive) {
printf("Drive: %s ", szSingleDrive); //输出单个驱动器的驱动器号
printf("\n");
putDrivesType(szSingleDrive); //输出逻辑驱动器类型
//GetDiskModelNumber(0, model_buf, sn_buf, fw_buf);
GetDiskModelNumber(count,driver, model_buf, sn_buf, fw_buf);
driver++;
printf("\n");
putDrivesFreeSpace(szSingleDrive); //获取磁盘空间信息
printf("----------------------------\n");
// 获取下一个驱动器号起始地址
szSingleDrive += strlen(szSingleDrive) + 1;
}
}
}
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);
capacity( model_buf, sn_buf, fw_buf);
// printf("Model: %s\nFirmware: %s\nSN: %s", model_buf, fw_buf, sn_buf);
getchar();
return 0;
}