メモリ使用量表示クラス


メモリ使用量表示クラス
class CMemoryUserge
{
protected:
  typedef struct _MEMORYSTATUS_EX {
    DWORD dwLength;             // sizeof(MEMORYSTATUSVLM) 
    DWORD dwMemoryLoad;         // percent of memory in use 
    DWORDLONG dwTotalPhys;     // bytes of physical memory 
    DWORDLONG dwAvailPhys;     // free physical memory bytes 
    DWORDLONG dwTotalPageFile; // bytes of paging file 
    DWORDLONG dwAvailPageFile; // free bytes of paging file 
    DWORDLONG dwTotalVirtual;  // user bytes of address space 
    DWORDLONG dwAvailVirtual;  // free user bytes 
    DWORDLONG dwAvailExtendedVirtual;
  } MEMORYSTATUS_EX, *LPMEMORYSTATUS_EX; 

  typedef VOID (WINAPI *GLOBALMEMORYSTATUSEX)( LPMEMORYSTATUS_EX lpBuffer );
  GLOBALMEMORYSTATUSEX pGlobalMemoryStatusVlm;
  MEMORYSTATUS_EX memStatus, memSave;
  
  CString PrintfULLData(ULONGLONG uLL)
  {
    char buffer[129]={0}; 
    _ui64toa(uLL, buffer, 10);
    
    char buffer2[129]={0};
    int i=0;
    int j=0;
    int k=strlen(buffer);
    int l=(k/3) + ((k%3)?1:0);
    int m=(k-l*3) > 0 ? (k-l*3):(3+(k-l*3));
    while(k--)
    {
      buffer2[i++] = buffer[j++];
      if((--m % 3 == 0) && k)
      {
        buffer2[i++] = ',';
        m=3;
      }
    }
    buffer2[i] = '\0';

    // XX 
    {
      k = strlen(buffer2);
      j = 18-k;
      if(j > 0)
      {
        strcpy(buffer, buffer2);
        memset(buffer2, 0, sizeof(buffer2));
        for(i=0; i<j; i++)
          buffer2[i] = ' ';
        strcat(buffer2, buffer);
      }      
    }
    
    return CString(buffer2);
  }

public:
  CMemoryUserge()
  {
    pGlobalMemoryStatusVlm = NULL;
    
    HMODULE hLib = LoadLibrary(_T("kernel32.dll"));
    if(hLib)
    {
      pGlobalMemoryStatusVlm = 
        (GLOBALMEMORYSTATUSEX)GetProcAddress(hLib, "GlobalMemoryStatusEx");
      FreeLibrary(hLib);
    }

    memset(&memSave, 0, sizeof(memSave));
    memset(&memStatus, 0, sizeof(memStatus));
  }

public:
  BOOL GetMemoryInfo(CString & szMemInfo, BOOL bForce=FALSE)
  {
    BOOL bUpdate=FALSE;
    
    if(pGlobalMemoryStatusVlm)
    {
      memStatus.dwLength = sizeof(memStatus);
      pGlobalMemoryStatusVlm( &memStatus );
    }
    else
    {
      MEMORYSTATUS tmpStatus={0};
      tmpStatus.dwLength = sizeof(tmpStatus);
      GlobalMemoryStatus( &tmpStatus );

      memStatus.dwMemoryLoad = tmpStatus.dwMemoryLoad;         // percent of memory in use 
      memStatus.dwTotalPhys = tmpStatus.dwTotalPhys;     // bytes of physical memory 
      memStatus.dwAvailPhys = tmpStatus.dwAvailPhys;     // free physical memory bytes 
      memStatus.dwTotalPageFile = tmpStatus.dwTotalPageFile; // bytes of paging file 
      memStatus.dwAvailPageFile = tmpStatus.dwAvailPageFile; // free bytes of paging file 
      memStatus.dwTotalVirtual = tmpStatus.dwTotalVirtual;  // user bytes of address space 
      memStatus.dwAvailVirtual = tmpStatus.dwAvailVirtual;  // free user bytes 
      memStatus.dwAvailExtendedVirtual = 0;
    }

    if(bForce || memcmp(&memSave, &memStatus, sizeof(memSave)))
    {
      szMemInfo.Format(
        _T("memory in use = %lu%%\r
") _T("total physical memory = %s\r
") _T("free physical memory = %s\r
") _T("total bytes of paging file = %s\r
") _T("free bytes of paging file = %s\r
") _T("user bytes of address space = %s\r
") _T("free in virtual address = %s\r
"), memStatus.dwMemoryLoad, PrintfULLData(memStatus.dwTotalPhys), PrintfULLData(memStatus.dwAvailPhys), PrintfULLData(memStatus.dwTotalPageFile), PrintfULLData(memStatus.dwAvailPageFile), PrintfULLData(memStatus.dwTotalVirtual), PrintfULLData(memStatus.dwAvailVirtual) ); memSave = memStatus; bUpdate = TRUE; } return bUpdate; } };