VC++ 怎样获取硬盘的物理序列号

如题所述

第1个回答  2017-08-24
//MyDiskInfo.h头文件#pragmaonceclassMyDiskInfo{public:MyDiskInfo(void);public:~MyDiskInfo(void);public:intGetDiskInfo(intdriver=0);charszModelNumber[64];charszSerialNumber[64];};//MyDiskInfo.cpp文件//MyDiskInfo.cpp:implementationoftheCMyDiskInfoclass.////////////////////////////////////////////////////////////////////////#include"stdafx.h"#include"MyDiskInfo.h"#ifdef_DEBUG#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#definenewDEBUG_NEW#endifconstWORDIDE_ATAPI_IDENTIFY=0xA1;//读取ATAPI设备的命令constWORDIDE_ATA_IDENTIFY=0xEC;//读取ATA设备的命令#define_WIN32_WINNT0x0400#include"winioctl.h"//#include"NTDDSCSI.h"////////////////////////////////////////////////////////////////////////Construction/Destruction//////////////////////////////////////////////////////////////////////MyDiskInfo::MyDiskInfo(void){}MyDiskInfo::~MyDiskInfo(void){}BOOL__fastcallDoIdentify(HANDLEhPhysicalDriveIOCTL,PSENDCMDINPARAMSpSCIP,PSENDCMDOUTPARAMSpSCOP,BYTEbtIDCmd,BYTEbtDriveNum,PDWORDpdwBytesReturned){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;returnDeviceIoControl(hPhysicalDriveIOCTL,SMART_RCV_DRIVE_DATA,(LPVOID)pSCIP,sizeof(SENDCMDINPARAMS)-1,(LPVOID)pSCOP,sizeof(SENDCMDOUTPARAMS)+IDENTIFY_BUFFER_SIZE-1,pdwBytesReturned,NULL);}char*__fastcallConvertToString(DWORDdwDiskData[256],intnFirstIndex,intnLastIndex){staticcharszResBuf[1024];charss[256];intnIndex=0;intnPosition=0;for(nIndex=nFirstIndex;nIndex>nDrive&0x10)?IDE_ATAPI_IDENTIFY:IDE_ATA_IDENTIFY;//输出参数BYTEbtIDOutCmd[sizeof(SENDCMDOUTPARAMS)+IDENTIFY_BUFFER_SIZE-1];if(DoIdentify(hFile,&InParams,(PSENDCMDOUTPARAMS)btIDOutCmd,(BYTE)btIDCmd,(BYTE)nDrive,&dwBytesReturned)==FALSE)return-3;::CloseHandle(hFile);DWORDdwDiskData[256];USHORT*pIDSector;//对应结构IDSECTOR,见头文件pIDSector=(USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer;for(inti=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));return0;}本回答被提问者采纳
第2个回答  2017-08-23
用GetVolumeInformation吧,下面是一段示例,不是我写的,应该有效。#include"stdafx.h"#includeusingnamespacestd;#includeLPTSTRnamebuf=newchar[12];DWORDnamesize=12;DWORDserialnumber;DWORDmaxlen;DWORDfileflag;LPTSTRsysnamebuf=newchar[10];DWORDsysnamesize=10;//intnum=m_disklist.GetItemCount();intmain(){::GetVolumeInformation("C:\\",namebuf,namesize,&serialnumber,&maxlen,&fileflag,sysnamebuf,sysnamesize);cout<
第3个回答  2017-05-08
int ioctl(int fd, unsigned long request, ...);