[分享] C++获取硬盘序列号

[复制链接]
  • TA的每日心情
    开心
    2014-4-2 21:30
  • 签到天数: 3 天

    [LV.2]九品芝麻官

    534

    主题

    400

    回帖

    7742

    积分

    Intohard Team

    Rank: 7Rank: 7Rank: 7

    积分
    7742
    发表于 2012-4-20 08:50:17 | 显示全部楼层 |阅读模式
    1. #include   <windows.h>
    2. #include   <iostream.h>
    3. #include   <stdio.h>

    4. #pragma argsused

    5. #define   DFP_GET_VERSION   0x00074080
    6. #define   DFP_SEND_DRIVE_COMMAND   0x0007c084
    7. #define   DFP_RECEIVE_DRIVE_DATA   0x0007c088

    8. //#pragma   pack(1)
    9. typedef   struct   _GETVERSIONOUTPARAMS   {
    10.     BYTE   bVersion;        //Binary   driver   version.
    11.     BYTE   bRevision;       //Binary   driver   revision.
    12.     BYTE   bReserved;       //Not   used.
    13.     BYTE   bIDEDeviceMap;   //Bit   map   of   IDE   devices.
    14.     DWORD   fCapabilities; //Bit   mask   of   driver   capabilities.
    15.     DWORD   dwReserved[4]; //For   future   use.
    16. }   GETVERSIONOUTPARAMS,   *PGETVERSIONOUTPARAMS,   *LPGETVERSIONOUTPARAMS;   

    17. typedef   struct   _IDEREGS   {
    18.     BYTE   bFeaturesReg;     //Used for   specifying   SMART   "commands".
    19.     BYTE   bSectorCountReg;   //IDE   sector   count   register
    20.     BYTE   bSectorNumberReg;   //IDE   sector   number   register
    21.     BYTE   bCylLowReg;       //   IDE   low   order   cylinder   value     
    22.     BYTE   bCylHighReg;     //   IDE   high   order   cylinder   value     
    23.     BYTE   bDriveHeadReg;     //   IDE   drive/head   register     
    24.     BYTE   bCommandReg;     //   Actual   IDE   command.     
    25.     BYTE   bReserved;       //   reserved   for   future   use.     Must   be   zero.     
    26. }   IDEREGS,   *PIDEREGS,   *LPIDEREGS;     
    27.    
    28. typedef   struct   _SENDCMDINPARAMS   {     
    29.     DWORD   cBufferSize;     //   Buffer   size   in   bytes     
    30.     IDEREGS   irDriveRegs;     //   Structure   with   drive   register   values.     
    31.     BYTE   bDriveNumber;     //   Physical   drive   number   to   send     
    32.                   //   command   to   (0,1,2,3).     
    33.     BYTE   bReserved[3];     //   Reserved   for   future   expansion.     
    34.     DWORD   dwReserved[4];     //   For   future   use.     
    35.     //BYTE     bBuffer[1];       //   Input   buffer.     
    36. }   SENDCMDINPARAMS,   *PSENDCMDINPARAMS,   *LPSENDCMDINPARAMS;     
    37.    
    38. typedef   struct   _DRIVERSTATUS   {     
    39.     BYTE   bDriverError;     //   Error   code   from   driver,     
    40.                   //   or   0   if   no   error.     
    41.     BYTE   bIDEStatus;       //   Contents   of   IDE   Error   register.     
    42.                   //   Only   valid   when   bDriverError     
    43.                   //   is   SMART_IDE_ERROR.     
    44.     BYTE   bReserved[2];     //   Reserved   for   future   expansion.     
    45.     DWORD   dwReserved[2];     //   Reserved   for   future   expansion.     
    46. }   DRIVERSTATUS,   *PDRIVERSTATUS,   *LPDRIVERSTATUS;     
    47.    
    48. typedef   struct   _SENDCMDOUTPARAMS   {     
    49.     DWORD         cBufferSize;     //   Size   of   bBuffer   in   bytes     
    50.     DRIVERSTATUS   DriverStatus;     //   Driver   status   structure.     
    51.     BYTE       bBuffer[512];       //   Buffer   of   arbitrary   length     
    52.                       //   in   which   to   store   the   data   read   from   the   drive.     
    53. }   SENDCMDOUTPARAMS,   *PSENDCMDOUTPARAMS,   *LPSENDCMDOUTPARAMS;     

    54. typedef   struct   _IDSECTOR   {     
    55.     USHORT   wGenConfig;     
    56.     USHORT   wNumCyls;     
    57.     USHORT   wReserved;     
    58.     USHORT   wNumHeads;     
    59.     USHORT   wBytesPerTrack;     
    60.     USHORT   wBytesPerSector;     
    61.     USHORT   wSectorsPerTrack;     
    62.     USHORT   wVendorUnique[3];     
    63.     CHAR   sSerialNumber[20];     
    64.     USHORT   wBufferType;     
    65.     USHORT   wBufferSize;     
    66.     USHORT   wECCSize;     
    67.     CHAR   sFirmwareRev[8];     
    68.     CHAR   sModelNumber[40];     
    69.     USHORT   wMoreVendorUnique;     
    70.     USHORT   wDoubleWordIO;     
    71.     USHORT   wCapabilities;     
    72.     USHORT   wReserved1;     
    73.     USHORT   wPIOTiming;     
    74.     USHORT   wDMATiming;     
    75.     USHORT   wBS;     
    76.     USHORT   wNumCurrentCyls;     
    77.     USHORT   wNumCurrentHeads;     
    78.     USHORT   wNumCurrentSectorsPerTrack;     
    79.     ULONG   ulCurrentSectorCapacity;     
    80.     USHORT   wMultSectorStuff;     
    81.     ULONG   ulTotalAddressableSectors;     
    82.     USHORT   wSingleWordDMA;     
    83.     USHORT   wMultiWordDMA;     
    84.     BYTE   bReserved[128];     
    85. }   IDSECTOR,   *PIDSECTOR;
    86.    
    87. /*+++     
    88. Global   vars     
    89. ---*/     
    90. GETVERSIONOUTPARAMS   vers;     
    91. SENDCMDINPARAMS   in;     
    92. SENDCMDOUTPARAMS   out;     
    93. HANDLE   h;     
    94. DWORD   i;
    95. BYTE   j;
    96. char HardDiskNO[200];

    97. VOID   ChangeByteOrder(PCHAR   szString,   USHORT   uscStrSize)
    98. {
    99. USHORT   i;
    100. CHAR   temp;   

    101. for   (i   =   0;   i   <   uscStrSize;   i+=2)
    102. {
    103.     temp   =   szString[i];
    104.     szString[i]   =   szString[i+1];
    105.     szString[i+1]   =   temp;
    106. }
    107. }
    108.    
    109. void   DetectIDE(BYTE bIDEDeviceMap){
    110. if   (bIDEDeviceMap&1){
    111.     if   (bIDEDeviceMap&16){
    112.       //cout<<"ATAPI   device   is   attached   to   primary   controller,   drive   0."<<endl;
    113.     }else{
    114.       //cout<<"IDE   device   is   attached   to   primary   controller,   drive   0."<<endl;
    115.     }
    116. }     
    117. if   (bIDEDeviceMap&2){     
    118.     if   (bIDEDeviceMap&32){     
    119.       //cout<<"ATAPI   device   is   attached   to   primary   controller,   drive   1."<<endl;
    120.     }else{
    121.       //cout<<"IDE   device   is   attached   to   primary   controller,   drive   1."<<endl;
    122.     }     
    123. }     
    124. if   (bIDEDeviceMap&4){     
    125.     if   (bIDEDeviceMap&64){
    126.       //cout<<"ATAPI   device   is   attached   to   secondary   controller,   drive   0."<<endl;
    127.     }else{     
    128.       //cout<<"IDE   device   is   attached   to   secondary   controller,   drive   0."<<endl;
    129.     }     
    130. }     
    131. if   (bIDEDeviceMap&8){
    132.     if   (bIDEDeviceMap&128){     
    133.       //cout<<"ATAPI   device   is   attached   to   secondary   controller,   drive   1."<<endl;
    134.     }else{     
    135.       //cout<<"IDE   device   is   attached   to   secondary   controller,   drive   1."<<endl;
    136.     }
    137. }     
    138. }     
    139.    
    140. void   hdid9x(){     
    141. ZeroMemory(&vers,sizeof(vers));
    142. //We   start   in   95/98/Me
    143. h=CreateFile("\\\\.\\Smartvsd",0,0,0,CREATE_NEW,0,0);
    144. if   (!h){
    145.     //cout<<"open   smartvsd.vxd   failed"<<endl;
    146.     exit(0);
    147. }

    148. if   (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
    149.     //cout<<"DeviceIoControl   failed:DFP_GET_VERSION"<<endl;
    150.     CloseHandle(h);
    151.     return;
    152. }
    153. //If   IDE   identify   command   not   supported,   fails     
    154. if   (!(vers.fCapabilities&1)){     
    155.     //cout<<"Error:   IDE   identify   command   not   supported.";
    156.     CloseHandle(h);     
    157.     return;     
    158. }
    159. //Display   IDE   drive   number   detected
    160. DetectIDE(vers.bIDEDeviceMap);
    161. //Identify   the   IDE   drives
    162. for   (j=0;j<4;j++){
    163.     PIDSECTOR   phdinfo;
    164.     char   s[41];
    165.         
    166.     ZeroMemory(&in,sizeof(in));     
    167.     ZeroMemory(&out,sizeof(out));     
    168.     if   (j&1){     
    169.       in.irDriveRegs.bDriveHeadReg=0xb0;     
    170.     }else{
    171.       in.irDriveRegs.bDriveHeadReg=0xa0;     
    172.     }     
    173.     if   (vers.fCapabilities&(16>>j)){     
    174.       //We   don't   detect   a   ATAPI   device.     
    175.       //cout<<"Drive   "<<(int)(j+1)<<"   is   a   ATAPI   device,   we   don't   detect   it"<<endl;
    176.       continue;
    177.     }else{     
    178.       in.irDriveRegs.bCommandReg=0xec;     
    179.     }     
    180.     in.bDriveNumber=j;     
    181.     in.irDriveRegs.bSectorCountReg=1;     
    182.     in.irDriveRegs.bSectorNumberReg=1;
    183.     in.cBufferSize=512;     
    184.     if   (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0)){     
    185.       //cout<<"DeviceIoControl   failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
    186.       CloseHandle(h);     
    187.       return;     
    188.     }
    189.     phdinfo=(PIDSECTOR)out.bBuffer;     
    190.     memcpy(s,phdinfo->sModelNumber,40);     
    191.     s[40]=0;     
    192.     ChangeByteOrder(s,40);     
    193.     //cout<<endl<<"Module   Number:"<<s<<endl;
    194.     memcpy(s,phdinfo->sFirmwareRev,8);
    195.     s[8]=0;     
    196.     ChangeByteOrder(s,8);     
    197.     //cout<<"\tFirmware   rev:"<<s<<endl;
    198.     memcpy(s,phdinfo->sSerialNumber,20);     
    199.     s[20]=0;     
    200.     ChangeByteOrder(s,20);
    201.     strcpy(HardDiskNO, s);
    202.     //cout<<"\tSerial   Number:"<<s<<endl;
    203.     //cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
    204. }     
    205.    
    206. //Close   handle   before   quit
    207. CloseHandle(h);
    208. }

    209. void hdidnt(){
    210. char hd[80];
    211. PIDSECTOR   phdinfo;
    212. char s[41];

    213. ZeroMemory(&vers,sizeof(vers));
    214. //We   start   in   NT/Win2000
    215. for (j = 0; j < 4; j++){
    216.     sprintf(hd,"\\\\.\\PhysicalDrive%d",j);
    217.     h = CreateFile(hd,GENERIC_READ|GENERIC_WRITE,
    218.         FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
    219.     if (!h) continue;
    220.     if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0))
    221.     {
    222.         CloseHandle(h);
    223.         continue;
    224.     }
    225.     //If   IDE   identify   command   not   supported,   fails
    226.     if (!(vers.fCapabilities&1)){
    227.       CloseHandle(h);
    228.       return;
    229.     }

    230.     //Identify   the   IDE   drives
    231.     ZeroMemory(&in,sizeof(in));
    232.     ZeroMemory(&out,sizeof(out));
    233.     if (j&1){
    234.       in.irDriveRegs.bDriveHeadReg=0xb0;
    235.     }
    236.     else
    237.     {
    238.       in.irDriveRegs.bDriveHeadReg=0xa0;
    239.     }
    240.     if (vers.fCapabilities&(16>>j)) {
    241.       //We   don't   detect   a   ATAPI   device.
    242.       //cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
    243.       continue;
    244.     }
    245.     else
    246.     {
    247.       in.irDriveRegs.bCommandReg=0xec;
    248.     }
    249.     in.bDriveNumber=j;
    250.     in.irDriveRegs.bSectorCountReg=1;
    251.     in.irDriveRegs.bSectorNumberReg=1;
    252.     in.cBufferSize=512;
    253.     if (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0)){
    254.       //cout<<"DeviceIoControl   failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
    255.       CloseHandle(h);
    256.       return;
    257.     }
    258.     phdinfo=(PIDSECTOR)out.bBuffer;
    259.     memcpy(s,phdinfo->sModelNumber,40);     
    260.     s[40]=0;
    261.     ChangeByteOrder(s,40);
    262.     //cout<<endl<<"Module   Number:"<<s<<endl;
    263.     memcpy(s,phdinfo->sFirmwareRev,8);     
    264.     s[8]=0;
    265.     ChangeByteOrder(s,8);

    266.     //cout<<"\tFirmware   rev:"<<s<<endl;
    267.     memcpy(&s[21],phdinfo->sSerialNumber,20);
    268.     s[40]=0;
    269.     ChangeByteOrder(&s[21],20);
    270.     //cout<<"\tSerial   Number:"<<s<<endl;
    271.     sprintf(s, "硬盘 %d 的序列号是:%s", j + 1, &s[21]);
    272.     strcpy(&HardDiskNO[j * 50], s);
    273.     //cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
    274.     CloseHandle(h);
    275. }
    276. }

    277. extern "C" {
    278.     char * _export _stdcall GetHardDiskNO();
    279. }
    280. //---------------------------------------------------------------------------

    281. char * _stdcall GetHardDiskNO()
    282. {
    283.     OSVERSIONINFO   VersionInfo;

    284.     ZeroMemory(&VersionInfo,sizeof(VersionInfo));
    285.     VersionInfo.dwOSVersionInfoSize=sizeof(VersionInfo);
    286.     GetVersionEx(&VersionInfo);

    287.     memset(HardDiskNO, 0, 200);

    288.     switch (VersionInfo.dwPlatformId)
    289.     {
    290.         case VER_PLATFORM_WIN32s:
    291.           break;
    292.         case VER_PLATFORM_WIN32_WINDOWS:
    293.           hdid9x();
    294.           break;
    295.         case VER_PLATFORM_WIN32_NT:
    296.           hdidnt();
    297.           break;
    298.     }

    299.     return HardDiskNO;
    300. }

    复制代码
  • TA的每日心情
    奋斗
    2013-10-23 16:36
  • 签到天数: 130 天

    [LV.7]四品道员

    2

    主题

    514

    回帖

    1004

    积分

    [INTOHARD]营长

    Rank: 6Rank: 6

    积分
    1004
    发表于 2013-7-9 17:48:11 | 显示全部楼层
    支持一下!多谢分享!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-12-12 18:49
  • 签到天数: 1 天

    [LV.1]布衣百姓

    17

    主题

    183

    回帖

    1235

    积分

    [INTOHARD]营长

    Rank: 6Rank: 6

    积分
    1235
    发表于 2013-11-27 18:13:39 | 显示全部楼层
    支持LZ,谢谢分享!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    快速回复 返回顶部 返回列表