CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2001
    Location
    Islamabad , QAU,Pakistan
    Posts
    61

    how to get system information programatically

    Hi

    I want to get system infrormation programatically
    I mean harddisk size, freespace, type ets.
    what api are available for this in vc++.
    can anybody tell me

  2. #2
    Join Date
    Jul 2002
    Location
    Scotland
    Posts
    288
    Hi,

    There is a system information component you can add to the about box of your project. Select Project / Add to Project > Components and Controls, and it's in the VC++ Components Dir.

    Or do a search in MSDN for system information there is sample code there.

    HTH
    --
    Regards,
    William.

    SELECT COUNT(*) FROM USERS WHERE KNOWLEDGE != NULL ;
    0 Rows Returned!

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...which kind of system information?? Below are some samples for different kind of information....oh and sorry for the lengthy post...

    1. Get available drives
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // Get logical drives
    DWORD dwLogicalDrives = ::GetLogicalDrives();
    if(dwLogicalDrives)
    {
      for(int iCnt = 0; iCnt < 32; ++iCnt)
      {
        memset(szDriveRoot, 0, sizeof(szDriveRoot));
    
        if(dwLogicalDrives & (1 << iCnt))
        {
          // Set drive root
          sprintf(szDriveRoot, "%c:\\", iCnt + 'A');
    
          // Determine partition type
          UINT uiDriveType = ::GetDriveType(szDriveRoot);
    
          switch(uiDriveType)
          {
            // Unknown
            case DRIVE_UNKNOWN:
              cout << "Partition " << szDriveRoot << " -> " << "Unknown" << endl;
              break;
    
            // Root path invalid
            case DRIVE_NO_ROOT_DIR:
              cout << "Partition " << szDriveRoot << " -> " << "Root path invalid" << endl;
              break;
    
            // Removable drive
            case DRIVE_REMOVABLE:
              cout << "Partition " << szDriveRoot << " -> " << "Removable drive" << endl;
              break;
    
            // Fixed drive
            case DRIVE_FIXED:
              cout << "Partition " << szDriveRoot << " -> " << "Fixed drive" << endl;
              break;
    
            // Network drive
            case DRIVE_REMOTE:
              cout << "Partition " << szDriveRoot << " -> " << "Network drive" << endl;
              break;
    
            // CD-ROM
            case DRIVE_CDROM:
              cout << "Partition " << szDriveRoot << " -> " << "CD-ROM" << endl;
              break;
    
            // RAM-Disk
            case DRIVE_RAMDISK:
              cout << "Partition " << szDriveRoot << " -> " << "RAM-Disk" << endl;
              break;
          }
        }
      }
    }
    2. Free space on drive
    Code:
    typedef BOOL (WINAPI *PFNGETDISKFREESPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
    
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    HINSTANCE hModule = ::LoadLibrary("KERNEL32.DLL");
    if(hModule)
    {
      PFNGETDISKFREESPACEEX pDiskFreeSpaceEx = NULL;
    
      // Determine function to use
      pDiskFreeSpaceEx = reinterpret_cast<PFNGETDISKFREESPACEEX>(::GetProcAddress(hModule, "GetDiskFreeSpaceExA"));
      if(!pDiskFreeSpaceEx)
      {
        DWORD dwSectorsPerCluster = 0;
        DWORD dwBytesPerSector    = 0; 
        DWORD dwFreeClusters      = 0; 
        DWORD dwClusters          = 0; 
    
        if(::GetDiskFreeSpace(strRootPath.c_str(),
                              &dwSectorsPerCluster,
                              &dwBytesPerSector,
                              &dwFreeClusters,
                              &dwClusters) == TRUE)
          cout << "Available disk space = " << dwFreeClusters * dwBytesPerSector * dwSectorsPerCluster << " MB" << endl;
        else
          // Error
      }
      else
      {
        ULARGE_INTEGER uliFreeBytesAvailableToCaller;
        ULARGE_INTEGER uliTotalNumberOfBytes;
        ULARGE_INTEGER uliTotalNumberOfFreeBytes;
    
        if(::GetDiskFreeSpaceEx(strRootPath.c_str(),
                                &uliFreeBytesAvailableToCaller,
                                &uliTotalNumberOfBytes,
                                &uliTotalNumberOfFreeBytes) == TRUE)
          cout << "Available disk space = " << static_cast<int>(static_cast<__int64>(uliFreeBytesAvailableToCaller.QuadPart / (1024 * 1024))) << " MB)" << endl;
        else
          // Error
      }
    
      // Release library
      ::FreeLibrary(hModule);
    }
    else
      // Error -> call ::GetLastError()
    3. Operating system
    Code:
    #include <windows.h>;
    
    OSVERSIONINFO OSInfo;
    
    memset(&OSInfo, 0, sizeof(OSInfo));
    
    // Set size
    OSInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    
    if(::GetVersionEx((OSVERSIONINFO *) &OSInfo) == FALSE)
      return false;
    
    switch(OSInfo.dwPlatformId)
    {
      case VER_PLATFORM_WIN32_NT:
        if(OSInfo.dwMajorVersion &lt;= 4)
          // Microsoft Windows NT
    
        if((OSInfo.dwMajorVersion == 5) && (!OSInfo.dwMinorVersion))
          // Microsoft Windows 2000
    
        if((OSInfo.dwMajorVersion == 5) && (OSInfo.dwMinorVersion == 1))
          // Microsoft Windows XP
    
        break;
    
      case VER_PLATFORM_WIN32_WINDOWS:
        if((OSInfo.dwMajorVersion == 4) && (!OSInfo.dwMinorVersion))
          if(OSInfo.szCSDVersion[1] == 'C')
            // Microsoft Windows 95 OSR2
          else
            // Microsoft Windows 95
    
        if((OSInfo.dwMajorVersion == 4) && (OSInfo.dwMinorVersion == 10))
          if(OSInfo.szCSDVersion[1] == 'A')
            // Microsoft Windows 98 SE
          else
            // Microsoft Windows 98
    
        if((OSInfo.dwMajorVersion == 4) && (OSInfo.dwMinorVersion == 90))
          // Microsoft Windows ME
    
        break;
    
      case VER_PLATFORM_WIN32s:
        // Microsoft Win32s
    
      break;
    }
    You can also take a look at http://www.codeproject.com/system/dtwinver.asp. This is one of the most comprehensive tool for OS detection I saw...


    4. Memory usage of process
    Code:
    #include <iostream>
    #include <iomanip>
    #include <psapi.h>
    
    // Add 'psapi.lib' to your linker options
    
    int main()
    {
      // Open current process
      HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ::GetCurrentProcessId());
      if(hProcess)
      {
        PROCESS_MEMORY_COUNTERS ProcessMemoryCounters;
    
        memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters));
    
        // Set size of structure
        ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters);
    
        // Get memory usage
        if(::GetProcessMemoryInfo(hProcess, &ProcessMemoryCounters, sizeof(ProcessMemoryCounters)) == TRUE)
        {
          std::cout << std::setfill('0') << std::hex
                    << "PageFaultCount: 0x" << std::setw(8) << ProcessMemoryCounters.PageFaultCount << std::endl
                    << "PeakWorkingSetSize: 0x" << std::setw(8) << ProcessMemoryCounters.PeakWorkingSetSize << std::endl
                    << "WorkingSetSize: 0x" << std::setw(8) << ProcessMemoryCounters.WorkingSetSize << std::endl
                    << "QuotaPeakPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPeakPagedPoolUsage << std::endl
                    << "QuotaPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPagedPoolUsage << std::endl
                    << "QuotaPeakNonPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPeakNonPagedPoolUsage << std::endl
                    << "QuotaNonPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaNonPagedPoolUsage << std::endl
                    << "PagefileUsage: 0x" << std::setw(8) << ProcessMemoryCounters.PagefileUsage << std::endl
                    << "PeakPagefileUsage: 0x" << std::setw(8) << ProcessMemoryCounters.PeakPagefileUsage <<std::endl;
        }
        else
          std::cout << "Could not get memory usage (Error: " << ::GetLastError() << ")" << std::endl;
    
        // Close process
        ::CloseHandle(hProcess);
      }
      else
        std::cout << "Could not open process (Error " << ::GetLastError() << ")" << std::endl;
    
      return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured