如何得到唯一的硬盘序号
blog.csdn/educast/article/details/8364373
2012
不正确的序列号
利用GetVolumelnformation获取的计算机硬盘卷序列号,不是正确的唯一序列号:
01.voidCWJGISApp::Register()
02.{
03. DWORDVolumeSerialNumber;
04. GetVolumeInformation("c:\\",NULL,NULL,&VolumeSeriaINumber,NULL,NULL,NULL,NULL);
05. charcharVolumeSerialNumber[l0];
06. itoa(VolumeSerlalNumber,charVoIumeSerialNumber,10);
07. CString strTemp;
08. strTemp.Format("%s",charVolumeSerialNumber);
09.}
该函数获取的序列号在硬盘格式化后会发生变化。
正确获取硬盘的序列号
正确获取硬盘序列号代码如下:
[cpp] view plain copy
print?
1.//读般硬盘序列号函数
2.char* CGetHDSerial::GetHDSerial()
3.{
4.m_buffer[0]='\n';
5.//得到当前操作系统版本
6.OSVERSIONINFO OSVersionInfo;
7.OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
8.GetVersionEx(&OSVersionInfo);
9.if(OSVersionInfo.dwPlatformld != VER_PLATFORM_WIN32_NT)
10.{
11.//Windows 9x/ME下读取硬盘序列号
12.WORD m_wWin9xl'HDSerial[256];
13.Win9xReadHDSerial(m_wWin9xHDSerial);
14.strcpy (m_buffer, WORDToChar(m _wWin9xHDSerial, 10, 19));
15.}
16.else
17.{
18.//Windows NT/2000/XP下读取硬盘序列弓
19.DWORD m_wWinNTHDSerial[256];
20.//判断是否有SCSI硬盘
21.if (!WinNTReadIDEHDSerial(m_wWinNTHDSerial))
22.WinNTReadSCSIHDSerial(m_wWinNTHDSerial);
23.strcpy (m_buffer,DWORDToChar(m_wWinNTHDSerial,10, 19));
硬盘序列号查询24.}
25.return m_buffer;
26.}
27.//Windows NT/2000/XP下读取IDE硬盘序列号
28.BOOL CGetHDScriaI::WinNTReadIDEHDSerial(DWORD* buffer)
29.{
30.BYTE JdOutCmd [sizeof(SENDCMDOUTPARAMS)+IDFNTIFY_BUFFER_SIZE - 1];
31.BOOL bFlag = FALSE;
32.int drive = 0;
33.Char driveName[256];
34.HANDLE hPhysicalDrivelOCTL=0;
35.
36.Sprintf(driveName,"\\\.\\PhysicalDrive%d",drive);
37.//Windows NT/2000/XP 下创建文件需要管理员权限
38.hPhysicalDriveIOCTL=CreateFile(driveName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
39.
40.if(hPhysicalDriveIOCTL!=INVALID_HANDLE_VALUE)
41.{
42.GETVERSIONOUTPARAMS VersionParams;
43.DWORD cbBytesReturned=0;
44.//得到驱动器的I/O控制器版本
45.memser((void*)&VersionParams,0,sizeof(VersionParams));
46.if(DeviceIoControl(hPhysicalDriveIOCTL,IOCTL_GET_VERSION,NULL,0,&VersionPaiams,sizeof(VersionParams),sizeof(VersionParams),
47.&cbBytesReturned,NULL))
48.{
49.if(VersionParams.bIDEDeviceMap>0)
50.{
51.BYTE bIDCmd=0;//IDE或者ATAPI识别命令
52.SENDCMDINPARAMS scip;
53.//如果驱动器是光驱,采用命令IDE_ATAPI_IDENTIFY,command,否则采用命令IDE_ATA_IDENTIFY读取驱动器信息
54.bIDCmd=(VersionParams.bIDEDeviceMap>>drive&0x10)? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
55.
56.memset(&scip,0,sizeof(scip));
57.memset(IdOutCmd,0,sizeof(IdOutCmd));
58.//获取驱动器信息
59.if(WinNTGetIDEHDInfo(hPhysicalDrivelOCTL,&scip,(PSENDCMDOUTPARMS)&IdOutCmd,(BYTE)bIDCmd,(BYTE)drive,&cbBytesReturned))
60.{
61.int m=0;
62.USHORT*pldSector=(USHORT*)((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer;
63.for(m=0;m<256;m++)
64.buffer[m]=pIdSector[m];
65.bFlag=TRUE; //读取硬盘信息成功
66.}
67.}
68.}
69.CloseHandle(hPhysicalDriyeIOCTL); //关闭句柄
70.}
71.Return bFlag;
72.}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论