2024年7月22日发(作者:司滢滢)
如何得到唯一的硬盘序号
/educast/article/details/8364373
2012
不正确的序列号
利用GetVolumelnformation获取的计算机硬盘卷序列号,不是
正确的唯一序列号:
JGISApp::Register()
02.{
03. DWORDVolumeSerialNumber;
04. GetVolumeInformation("c:",NULL,NULL,&VolumeSeri
aINumber,NULL,NULL,NULL,NULL);
05. charcharVolumeSerialNumber[l0];
06. itoa(VolumeSerlalNumber,charVoIumeSerialNumber,10
);
07. CString strTemp;
08. ("%s",charVolumeSerialNumber);
09.}
该函数获取的序列号在硬盘格式化后会发生变化。
正确获取硬盘的序列号
正确获取硬盘序列号代码如下:
[cpp] view plain copy
print?
1. //读般硬盘序列号函数
2. char* CGetHDSerial::GetHDSerial()
3. {
4. m_buffer[0]='n';
5. //得到当前操作系统版本
6. OSVERSIONINFO OSVersionInfo;
7. rsionInfoSize = sizeof(OSVERSION
INFO);
8. GetVersionEx(&OSVersionInfo);
9. if(formld != 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, 1
0, 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,1
0, 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)+IDF
NTIFY_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,N
ULL,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(VersionParam
s));
46. if(DeviceIoControl(hPhysicalDriveIOCTL,IOCTL_GET_VE
RSION,NULL,0,&VersionPaiams,sizeof(VersionParams),sizeof(Ver
sionParams),
47. &cbBytesReturned,NULL))
48. {
49. if(viceMap>0)
50. {
51. BYTE bIDCmd=0;//IDE或者ATAPI识别命令
52. SENDCMDINPARAMS scip;
53. //如果驱动器是光驱,采用命令IDE_ATAPI_IDENTIFY,
command,否则采用命令IDE_ATA_IDENTIFY读取驱动器信息
54. bIDCmd=(viceMap>>drive&0x
10)? 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,(PS
ENDCMDOUTPARMS)&IdOutCmd,(BYTE)bIDCmd,(BYTE)drive,&c
bBytesReturned))
60. {
61. int m=0;
62. USHORT*pldSector=(USHORT*)((PSENDCMDOUTPAR
AMS)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. }
2024年7月22日发(作者:司滢滢)
如何得到唯一的硬盘序号
/educast/article/details/8364373
2012
不正确的序列号
利用GetVolumelnformation获取的计算机硬盘卷序列号,不是
正确的唯一序列号:
JGISApp::Register()
02.{
03. DWORDVolumeSerialNumber;
04. GetVolumeInformation("c:",NULL,NULL,&VolumeSeri
aINumber,NULL,NULL,NULL,NULL);
05. charcharVolumeSerialNumber[l0];
06. itoa(VolumeSerlalNumber,charVoIumeSerialNumber,10
);
07. CString strTemp;
08. ("%s",charVolumeSerialNumber);
09.}
该函数获取的序列号在硬盘格式化后会发生变化。
正确获取硬盘的序列号
正确获取硬盘序列号代码如下:
[cpp] view plain copy
print?
1. //读般硬盘序列号函数
2. char* CGetHDSerial::GetHDSerial()
3. {
4. m_buffer[0]='n';
5. //得到当前操作系统版本
6. OSVERSIONINFO OSVersionInfo;
7. rsionInfoSize = sizeof(OSVERSION
INFO);
8. GetVersionEx(&OSVersionInfo);
9. if(formld != 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, 1
0, 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,1
0, 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)+IDF
NTIFY_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,N
ULL,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(VersionParam
s));
46. if(DeviceIoControl(hPhysicalDriveIOCTL,IOCTL_GET_VE
RSION,NULL,0,&VersionPaiams,sizeof(VersionParams),sizeof(Ver
sionParams),
47. &cbBytesReturned,NULL))
48. {
49. if(viceMap>0)
50. {
51. BYTE bIDCmd=0;//IDE或者ATAPI识别命令
52. SENDCMDINPARAMS scip;
53. //如果驱动器是光驱,采用命令IDE_ATAPI_IDENTIFY,
command,否则采用命令IDE_ATA_IDENTIFY读取驱动器信息
54. bIDCmd=(viceMap>>drive&0x
10)? 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,(PS
ENDCMDOUTPARMS)&IdOutCmd,(BYTE)bIDCmd,(BYTE)drive,&c
bBytesReturned))
60. {
61. int m=0;
62. USHORT*pldSector=(USHORT*)((PSENDCMDOUTPAR
AMS)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. }