GetHardSoftInfo.cpp
上传用户:szjkjd
上传日期:2022-06-27
资源大小:8968k
文件大小:13k
源码类别:

浏览器

开发平台:

Visual C++

  1. /*! @GetHardSoftInfo.cpp
  2. *******************************************************************************
  3. <PRE>
  4. 模块名         : 获取计算机硬件信息
  5. 文件名         : GetHardSoftInfo.cpp
  6. 相关文件       : GetHardSoftInfo.h
  7. 文件实现功能   : 获取计算机硬件信息
  8. 作者           : 王小静
  9. 版本           : 1.0
  10. --------------------------------------------------------------------------------
  11. 备注           : 该类引于第三方文件
  12. --------------------------------------------------------------------------------
  13. 修改记录 : 
  14. 日  期          版本            修改人          修改内容 
  15. 2010/02/08      1.0             王小静            创建
  16. </PRE>
  17. ********************************************************************************
  18. * 版权所有(c) 
  19. *******************************************************************************/
  20. #include "stdafx.h"
  21. #include "GetHardSoftInfo.h"
  22. CGetMachineInfo::CGetMachineInfo(void)
  23. {
  24. OSVERSIONINFO version; memset (&version, 0, sizeof (version)); version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); GetVersionEx (&version); if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)//nt {
  25.      ReadPhysicalDriveInNT();
  26. }
  27. if(version.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)//9x
  28. {
  29. ReadDrivePortsInWin9X();
  30. }
  31. }
  32. BOOL CGetMachineInfo::ReturnInfo(int drive, DWORD diskdata [256])
  33. {
  34. char string1 [1024];    __int64 sectors = 0;    __int64 bytes = 0;       //  copy the hard drive serial number to the buffer    strcpy (string1, ConvertToString (diskdata, 10, 19));    if (0 == HardDriveSerialNumber [0] &&             //  serial number must be alphanumeric             //  (but there can be leading spaces on IBM drives)        (isalnum (string1 [0]) || isalnum (string1 [19])))       strcpy (HardDriveSerialNumber, string1); //#ifdef PRINTING_TO_CONSOLE_ALLOWED    switch (drive / 2)    {       case 0: str_HardDesk_Form ="Primary Controller";               break;       case 1: str_HardDesk_Form ="Secondary Controller";               break;       case 2: str_HardDesk_Form ="Tertiary Controller";               break;       case 3: str_HardDesk_Form ="Quaternary Controller";               break;    } //MessageBox(NULL,str_HardDesk_Form,NULL,NULL);    switch (drive % 2)    {       case 0: str_Controller ="Master drive";               break;       case 1: str_Controller ="Slave drive";               break;    }    str_DN_Modol =CString(ConvertToString (diskdata, 27, 46));       str_DN_Serial =CString(ConvertToString (diskdata, 10, 19));    str_DN_ControllerRevision =CString(ConvertToString (diskdata, 23, 26));    str_HardDeskBufferSize.Format("%u", diskdata [21] * 512);    if (diskdata [0] & 0x0080)       str_HardDeskType ="Removable";    else if (diskdata [0] & 0x0040)       str_HardDeskType ="Fixed";    else str_HardDeskType ="Unknown";             //  calculate size based on 28 bit or 48 bit addressing //  48 bit addressing is reflected by bit 10 of word 83 if (diskdata [83] & 0x400)  sectors = diskdata [103] * 65536I64 * 65536I64 * 65536I64 +  diskdata [102] * 65536I64 * 65536I64 +  diskdata [101] * 65536I64 +  diskdata [100]; else sectors = diskdata [61] * 65536 + diskdata [60]; //  there are 512 bytes in a sector bytes = sectors * 512/1024/1024/1024; str_HardDeskSize.Format("%I64dGB",bytes); return 1;
  35. }
  36. //conversion to char 
  37. char *CGetMachineInfo::ConvertToString(DWORD diskdata [256], int firstIndex, int lastIndex)
  38. {
  39. static char string [1024];    int index = 0;    int position = 0;       //  each integer has two characters stored in it backwards    for (index = firstIndex; index <= lastIndex; index++)    {          //  get high byte for 1st character       string [position] = (char) (diskdata [index] / 256);       position++;          //  get low byte for 2nd character       string [position] = (char) (diskdata [index] % 256);       position++;    }       //  end the string     string [position] = '';       //  cut off the trailing blanks    for (index = position - 1; index > 0 && ' ' == string [index]; index--)       string [index] = '';    return string;
  40. }
  41. //
  42.  CGetMachineInfo::DoIDENTIFY(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,                  PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,                  PDWORD lpcbBytesReturned)
  43. {
  44.  // Set up data structures for IDENTIFY command.    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;       // Compute the drive number.    pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);       // The command can either be IDE identify or ATAPI identify.    pSCIP -> irDriveRegs.bCommandReg = bIDCmd;    pSCIP -> bDriveNumber = bDriveNum;    pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;    return( DeviceIoControl (hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA,                (LPVOID) pSCIP,                sizeof(SENDCMDINPARAMS) - 1,                (LPVOID) pSCOP,                sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,                lpcbBytesReturned, NULL) );
  45. }
  46. //
  47. int CGetMachineInfo::ReadPhysicalDriveInNT(void)
  48. {
  49. int done = FALSE;    int drive = 0;    for (drive = 0; drive < MAX_IDE_DRIVES; drive++)    {       HANDLE hPhysicalDriveIOCTL = 0;          //  Try to get a handle to PhysicalDrive IOCTL, report failure          //  and exit if can't.       char driveName [256];       sprintf (driveName, "\\.\PhysicalDrive%d", drive);          //  Windows NT, Windows 2000, must have admin rights       hPhysicalDriveIOCTL = CreateFile (driveName,                                GENERIC_READ | GENERIC_WRITE,                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,                                OPEN_EXISTING, 0, NULL);       // if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)       //    printf ("Unable to open physical drive %d, error code: 0x%lXn",       //            drive, GetLastError ());       if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)       {          GETVERSIONOUTPARAMS VersionParams;          DWORD               cbBytesReturned = 0;             // Get the version, etc of PhysicalDrive IOCTL          memset ((void*) &VersionParams, 0, sizeof(VersionParams));          if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,                    NULL,                     0,                    &VersionParams,                    sizeof(VersionParams),                    &cbBytesReturned, NULL) )          {                      // printf ("DFP_GET_VERSION failed for drive %dn", i);             // continue;          }             // If there is a IDE device at number "i" issue commands             // to the device          if (VersionParams.bIDEDeviceMap > 0)          {             BYTE             bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd             SENDCMDINPARAMS  scip;             //SENDCMDOUTPARAMS OutCmd; BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; // Now, get the ID sector for all IDE devices in the system.                // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,                // otherwise use the IDE_ATA_IDENTIFY command             bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ?                        IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;             memset (&scip, 0, sizeof(scip));             memset (IdOutCmd, 0, sizeof(IdOutCmd));             if ( DoIDENTIFY (hPhysicalDriveIOCTL,                         &scip,                         (PSENDCMDOUTPARAMS)&IdOutCmd,                         (BYTE) bIDCmd,                        (BYTE) drive,                        &cbBytesReturned))             {                DWORD diskdata [256];                int ijk = 0;                USHORT *pIdSector = (USHORT *)                              ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;                for (ijk = 0; ijk < 256; ijk++)                   diskdata [ijk] = pIdSector [ijk];                ReturnInfo (drive, diskdata);                done = TRUE;             }     }          CloseHandle (hPhysicalDriveIOCTL);       }    }    return done;
  50. }
  51. //
  52. int CGetMachineInfo::ReadDrivePortsInWin9X(void)
  53. {
  54. int done = FALSE;    HANDLE VxDHandle = 0;    pt_IdeDInfo pOutBufVxD = 0;    DWORD lpBytesReturned = 0;   //  set the thread priority high so that we get exclusive access to the disk    SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS);     // 1. Make an output buffer for the VxD    rt_IdeDInfo info;    pOutBufVxD = &info;       // *****************       // KLUDGE WARNING!!!       // HAVE to zero out the buffer space for the IDE information!       // If this is NOT done then garbage could be in the memory       // locations indicating if a disk exists or not.    ZeroMemory (&info, sizeof(info));    VxDHandle = CreateFile ("\\.\IDE21201.VXD", 0, 0, 0, 0, FILE_FLAG_DELETE_ON_CLOSE, 0);    if (VxDHandle != INVALID_HANDLE_VALUE)    {          // 2. Run VxD function       DeviceIoControl (VxDHandle, m_cVxDFunctionIdesDInfo, 0, 0, pOutBufVxD, sizeof(pt_IdeDInfo), &lpBytesReturned, 0);          // 3. Unload VxD       CloseHandle (VxDHandle);    }    else MessageBox (NULL, "ERROR: Could not open IDE21201.VXD file",  TITLE, MB_ICONSTOP);       // 4. Translate and store data    unsigned long int i = 0;    for (i=0; i<8; i++)    {       if((pOutBufVxD->DiskExists[i]) && (pOutBufVxD->IDEExists[i/2]))       { DWORD diskinfo [256]; for (int j = 0; j < 256; j++)  diskinfo [j] = pOutBufVxD -> DisksRawInfo [i * 256 + j];             // process the information for this buffer    ReturnInfo (i, diskinfo); done = TRUE;       }    } //  reset the thread priority back to normal    // SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_NORMAL);    SetPriorityClass (GetCurrentProcess (), NORMAL_PRIORITY_CLASS);    return done;
  55. }
  56. //
  57. int CGetMachineInfo::ReadIdeDriveAsScsiDriveInNT(void)
  58. {
  59. int done = FALSE;    int controller = 0;    for (controller = 0; controller < 2; controller++)    {       HANDLE hScsiDriveIOCTL = 0;       char   driveName [256];          //  Try to get a handle to PhysicalDrive IOCTL, report failure          //  and exit if can't.       sprintf (driveName, "\\.\Scsi%d:", controller);          //  Windows NT, Windows 2000, any rights should do       hScsiDriveIOCTL = CreateFile (driveName,                                GENERIC_READ | GENERIC_WRITE,                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,                                OPEN_EXISTING, 0, NULL);       // if (hScsiDriveIOCTL == INVALID_HANDLE_VALUE)       //    printf ("Unable to open SCSI controller %d, error code: 0x%lXn",       //            controller, GetLastError ());       if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE)       {          int drive = 0;          for (drive = 0; drive < 2; drive++)          {             char buffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];             SRB_IO_CONTROL *p = (SRB_IO_CONTROL *) buffer;             SENDCMDINPARAMS *pin =                    (SENDCMDINPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));             DWORD dummy;                 memset (buffer, 0, sizeof (buffer));             p -> HeaderLength = sizeof (SRB_IO_CONTROL);             p -> Timeout = 10000;             p -> Length = SENDIDLENGTH;             p -> ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;             strncpy ((char *) p -> Signature, "SCSIDISK", 8);                pin -> irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;             pin -> bDriveNumber = drive;             if (DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,                                   buffer,                                  sizeof (SRB_IO_CONTROL) +                                          sizeof (SENDCMDINPARAMS) - 1,                                  buffer,                                  sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,                                  &dummy, NULL))             {                SENDCMDOUTPARAMS *pOut =                     (SENDCMDOUTPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));                IDSECTOR *pId = (IDSECTOR *) (pOut -> bBuffer);                if (pId -> sModelNumber [0])                {                   DWORD diskdata [256];                   int ijk = 0;                   USHORT *pIdSector = (USHORT *) pId;                              for (ijk = 0; ijk < 256; ijk++)                      diskdata [ijk] = pIdSector [ijk];                   ReturnInfo (controller * 2 + drive, diskdata);                   done = TRUE;                }             }          }          CloseHandle (hScsiDriveIOCTL);       }    }    return done;
  60. }
  61. //