Solved

Question on system info

Posted on 1998-11-10
5
291 Views
Last Modified: 2013-11-20
Hi there,
          How can I use GetSystemInfo(); in MFC to get the system info and display it using MessageBox();

Any help offered is very much appreciated!
0
Comment
Question by:justinng
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 100 total points
ID: 1324457
This is taken directly from actual code, slightly modified:

   SYSTEM_INFO m_SystemInfo;

   . . .

   ::GetSystemInfo(&m_SystemInfo);

   CString s;

   switch (m_SystemInfo.wProcessorArchitecture)
   {
   case PROCESSOR_ARCHITECTURE_INTEL:
      s = _T("Intel");
      switch (m_SystemInfo.wProcessorLevel)
      {
      case 0:
      case 1:
      case 2:
         break;
      case 3:
         s += _T(" 80386");
      case 4:
         s += _T(" 80486");
         break;
      case 5:
         s += _T(" Pentium");
         break;
      default:
         {
            CString sTemp;
            sTemp.Format(_T(" x86 Family %hu"), m_SystemInfo.wProcessorLevel);
            s += sTemp;
         }
         break;
      }
      if (m_SystemInfo.wProcessorLevel == 3 || m_SystemInfo.wProcessorLevel == 4)
      {
         if (((m_SystemInfo.wProcessorRevision >> 8) & 0xFF) == 0xFF)
         {
            CString sTemp;
            sTemp.Format(_T("-%02X"), UINT(m_SystemInfo.wProcessorRevision & 0xFF));
            s += sTemp;
         }
         else
         {
            CString sTemp;
            sTemp.Format(_T("-%c%u"), TCHAR(_T('A') + ((m_SystemInfo.wProcessorRevision >> 8) & 0xFF)), UINT(m_SystemInfo.wProcessorRevision & 0xFF));
            s += sTemp;
         }
      }
      else if (m_SystemInfo.wProcessorLevel >= 5)
      {
         CString sTemp;
         sTemp.Format(_T(" Model %u Stepping %u"), UINT((m_SystemInfo.wProcessorRevision >> 8) & 0xFF), UINT(m_SystemInfo.wProcessorRevision & 0xFF));
         s += sTemp;
      }
      break;
   case PROCESSOR_ARCHITECTURE_MIPS:
      s = _T("Processor Architecture: PROCESSOR_ARCHITECTURE_MIPS");
      break;
   case PROCESSOR_ARCHITECTURE_ALPHA:
      s = _T("Processor Architecture: PROCESSOR_ARCHITECTURE_ALPHA");
      break;
   case PROCESSOR_ARCHITECTURE_PPC:
      s = _T("Processor Architecture: PROCESSOR_ARCHITECTURE_PPC");
      break;
   case PROCESSOR_ARCHITECTURE_UNKNOWN:
      s = _T("Processor Architecture: PROCESSOR_ARCHITECTURE_UNKNOWN");
      break;
   default:
      break;
   }

   ::MessageBox(hWnd, s, _T("SYSTEM_INFO"), MB_OK);

Just replace the code for CString with your own string handling.
0
 

Author Comment

by:justinng
ID: 1324458
Thanks stefanr! Is it also possible to get informations like no. of drives, diskspace etc??

0
 
LVL 3

Expert Comment

by:stefanr
ID: 1324459
Yes, you can call CreateFile on the device \\.\PHYSICALDRIVEn, where n is the number 0 and up, to obtain physical characteristics of the hard disks using the DeviceIoControl call. Indirectly you get the number of physical disks by counting the number of times CreateFile succeeds. Don't forget to call CloseHandle when done with a disk, though.
You enumerates the names of the logical drives by calling GetLogicalDriveStrings, where you get a double-zero terminated string of zero-terminated substrings, or GetLogicalDrives to retrieve a bitmask of at most 32 logical drives.
You retrieves the free space on a logical drive with the GetDiskFreeSpaceEx function.
And if you're working with NT you can use the performance data to retrieve all kind of statistics (dynamic data) about the system.
0
 

Author Comment

by:justinng
ID: 1324460
Is there any code segments to illustrate? thanks! I'm new to MFC programming....
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1324461
This is copied directly from actual code:

   struct LOGDRIVEINFO
   {
      BOOL m_bInfoValid;
      CString m_sDrive;
      CString m_sVolumeName;
      DWORD m_dwVolumeSerialNumber;
      DWORD m_dwMaxNameLength;
      DWORD m_dwFileSystemFlags;
      CString m_sFileSystemName;
      UINT m_nDriveType;
      unsigned long m_nSectorsPerCluster;
      unsigned long m_nBytesPerSector;
      unsigned long m_nFreeClusters;
      unsigned long m_nClusters;
      ...
   };

   CArray<LOGDRIVEINFO, LPCTSTR> m_arrLogicalDrives;

----

   const int MAXPHYDRIVE = 16;

   const CString sFileNamePrefix = _T("\\\\.\\");
   CString sFileName;
   CString sFilePath;
   HANDLE hFile = INVALID_HANDLE_VALUE;
   for (int i = 0; i < MAXPHYDRIVE; i++)
   {
      sFileName.Format(_T("PHYSICALDRIVE%d"), i);
      sFilePath = sFileNamePrefix + sFileName;
      hFile = ::CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (hFile != INVALID_HANDLE_VALUE)
      {
         int nIndex = m_arrPhysicalDrives.Add(sFileName);
         PHYDRIVEINFO& rPhyDriveInfo = m_arrPhysicalDrives.ElementAt(nIndex);
         DWORD dwBytesReturned = 0;
         ::DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &rPhyDriveInfo.m_DiskGeometry, sizeof(DISK_GEOMETRY), &dwBytesReturned, NULL);

         int cPartns = 4;
         while (TRUE)
         {
            rPhyDriveInfo.m_arrDriveLayout.SetSize(sizeof(DRIVE_LAYOUT_INFORMATION) + cPartns*sizeof(PARTITION_INFORMATION));
            ::ZeroMemory(rPhyDriveInfo.m_arrDriveLayout.GetData(), rPhyDriveInfo.m_arrDriveLayout.GetSize());
            if (::DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_LAYOUT, NULL, 0, rPhyDriveInfo.m_arrDriveLayout.GetData(), rPhyDriveInfo.m_arrDriveLayout.GetSize(), &dwBytesReturned, NULL))
               break;
            else if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
            {
               rPhyDriveInfo.m_arrDriveLayout.RemoveAll();
               break;
            }
            cPartns += 4;
         }

         ::CloseHandle(hFile);
      }
   }

----

   const int LOGDRIVEBUFSIZE = 1024;
   LPTSTR lpszzLogicalDrives = new TCHAR[LOGDRIVEBUFSIZE];
   ::ZeroMemory(lpszzLogicalDrives, LOGDRIVEBUFSIZE * sizeof(TCHAR));
   ::GetLogicalDriveStrings(LOGDRIVEBUFSIZE, lpszzLogicalDrives);

   for (LPTSTR lpsz = lpszzLogicalDrives; *lpsz != 0; lpsz += ::_tcslen(lpsz)+1)
   {
      TCHAR szLogicalDrive[4] = { 0 };
      ::_tcsnccpy(szLogicalDrive, lpsz, ::_tcslen(lpsz)-1);
      int nIndex = m_arrLogicalDrives.Add(szLogicalDrive);
      m_arrLogicalDrives[nIndex].m_bInfoValid = ::GetVolumeInformation(lpsz,
                             m_arrLogicalDrives[nIndex].m_sVolumeName.GetBuffer(_MAX_PATH), _MAX_PATH,
                             &m_arrLogicalDrives[nIndex].m_dwVolumeSerialNumber,
                             &m_arrLogicalDrives[nIndex].m_dwMaxNameLength,
                             &m_arrLogicalDrives[nIndex].m_dwFileSystemFlags,
                             m_arrLogicalDrives[nIndex].m_sFileSystemName.GetBuffer(_MAX_PATH), _MAX_PATH);
      m_arrLogicalDrives[nIndex].m_sVolumeName.ReleaseBuffer();
      m_arrLogicalDrives[nIndex].m_sFileSystemName.ReleaseBuffer();
      m_arrLogicalDrives[nIndex].m_nDriveType = ::GetDriveType(lpsz);
      if (m_arrLogicalDrives[nIndex].m_nDriveType == DRIVE_FIXED || m_arrLogicalDrives[nIndex].m_nDriveType == DRIVE_CDROM || m_arrLogicalDrives[nIndex].m_nDriveType == DRIVE_RAMDISK)
      {
         ::GetDiskFreeSpace(lpsz, &m_arrLogicalDrives[nIndex].m_nSectorsPerCluster, &m_arrLogicalDrives[nIndex].m_nBytesPerSector,
                                  &m_arrLogicalDrives[nIndex].m_nFreeClusters, &m_arrLogicalDrives[nIndex].m_nClusters);
      }
   }
   delete[] lpszzLogicalDrives;
   lpszzLogicalDrives = NULL;

----

   CString sRootPathName;
   for (int i = 0; i <= m_arrLogicalDrives.GetUpperBound(); i++)
   {
      if (m_arrLogicalDrives[i].m_nDriveType == DRIVE_FIXED || m_arrLogicalDrives[i].m_nDriveType == DRIVE_CDROM || m_arrLogicalDrives[i].m_nDriveType == DRIVE_RAMDISK)
      {
         sRootPathName = m_arrLogicalDrives[i].m_sDrive + _T("\\");
         ::GetDiskFreeSpace(sRootPathName, &m_arrLogicalDrives[i].m_nSectorsPerCluster, &m_arrLogicalDrives[i].m_nBytesPerSector,
                                  &m_arrLogicalDrives[i].m_nFreeClusters, &m_arrLogicalDrives[i].m_nClusters);
      }
   }

----

Sorry, but performance data is much too complicated to exemplify. If you want that, you have to search the online documentation (keywords "performance data") and study it from the ground up yourself. You can also look at Performance Data Helper to see if it has something to give.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question