取硬盘型号参考例子1

#include <stdio.h>
#include <Windows.h>
#include <WinIoCtl.h>
#include <tchar.h>
#include <assert.h>

const  WORD IDE_ATAPI_IDENTIFY=0xA1;   // 读取ATAPI设备的命令
const  WORD IDE_ATA_IDENTIFY=0xEC;   // 读取ATA设备的命令

char szModelNumber[64]={0};
char szSerialNumber[64]={0};
bool GetDiskInfo(int driver=0);
BOOL __fastcall DoIdentify(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,PSENDCMDOUTPARAMS pSCOP,BYTE btIDCmd,BYTE btDriveNum,PDWORD pdwBytesReturned);
char *__fastcall ConvertToString(DWORD dwDiskData[256], int nFirstIndex, int nLastIndex);

int main (void)
{
	GetDiskInfo();
	printf("取系列号%s\n",szSerialNumber);
	printf("取模型号%s\n",szModelNumber);
	return 0;
}


bool GetDiskInfo(int driver)
{
	//设备判断
	assert(driver>=0);if (driver<0)return false;

	TCHAR szText[64]={0};
	_sntprintf_s(szText,ARRAYSIZE(szText),TEXT("\\\\.\\PHYSICALDRIVE%d"),driver);

	HANDLE hFile = INVALID_HANDLE_VALUE;
	hFile = CreateFile(szText, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,0, NULL);
	if (hFile == INVALID_HANDLE_VALUE)	return false;

	DWORD dwBytesReturned;
	GETVERSIONINPARAMS gvopVersionParams;
	if (DeviceIoControl(hFile, SMART_GET_VERSION,NULL, 0, &gvopVersionParams,sizeof(gvopVersionParams),&dwBytesReturned,NULL)==NULL)
	{
		int a=GetLastError();
		assert(false);
		return false;
	}

	//if(gvopVersionParams.bIDEDeviceMap <= 0)	return -2;

	// IDE or ATAPI IDENTIFY cmd
	int btIDCmd = 0;
	SENDCMDINPARAMS InParams;
	int nDrive =0;
	btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10)?IDE_ATAPI_IDENTIFY:IDE_ATA_IDENTIFY;
	//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)
	{
		int a=GetLastError();
		assert(false);
		return false;
	}
	::CloseHandle(hFile);

	DWORD dwDiskData[256]={0};
	USHORT *pIDSector=NULL; // 对应结构IDSECTOR,见头文件

	pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer;
	for(int i=0; i < 256; i++)	dwDiskData[i] = pIDSector[i];

	// 取系列号
	ZeroMemory(szSerialNumber, sizeof(szSerialNumber));
	strcpy_s(szSerialNumber, ConvertToString(dwDiskData, 10, 19));

	// 取模型号
	ZeroMemory(szModelNumber, sizeof(szModelNumber));
	strcpy_s(szModelNumber, ConvertToString(dwDiskData, 27, 46));

	return true;
}

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]={0};
	char ss[256];
	int nIndex = 0;
	int nPosition = 0;

	for(nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex++)
	{
		ss[nPosition] = (char)(dwDiskData[nIndex] / 256);
		nPosition++;
		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;
}

/*
2015年5月6日 11:18:30
程序执行结果如下:
取系列号WD-WCC3F3538344
取模型号WDCWD10EZEX-00BN5A0
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值