Page 1 of 1

[WinAPI] Cata memorie RAM este in sistem?

Posted: 24 Aug 2009, 00:16
by Ovidiu Cucu
Intrebare: Cum se poate afla marimea memoriei fizice (RAM) din sistem?

Raspuns: Folosing functia GlobalMemoryStatus sau GlobalMemoryStatusEx.

Exemple

Code: Select all

#include <windows.h>
#include <stdio.h>

DWORD GetTotalPhysicalMemory()
{
   
   MEMORYSTATUS memStatus = {0};
   ::GlobalMemoryStatus(&memStatus);
   return memStatus.dwTotalPhys;
}

int main()
{
   const DWORD dwMBFactor = 0x00100000;
   DWORD dwTotalPhys = GetTotalPhysicalMemory();
   printf("Total physical memory: %u MB", dwTotalPhys / dwMBFactor);

   system("pause");
   return 0;
}

Code: Select all

#include <windows.h>
#include <stdio.h>

DWORDLONG GetTotalPhysicalMemory()
{
   MEMORYSTATUSEX memStatusEx = {0};
   memStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
   BOOL bRet = ::GlobalMemoryStatusEx(&memStatusEx);
   return memStatusEx.ullTotalPhys;
}

int main()
{
   const DWORD dwMBFactor = 0x00100000;
   DWORDLONG dwTotalPhys = GetTotalPhysicalMemory();
   printf("Total physical memory: %u MB", dwTotalPhys / dwMBFactor);

   system("pause");
   return 0;
}
Note
  • GlobalMemoryStatusEx este de preferat in locul GlobalMemoryStatus pentru ca
    • Pentru ca GlobalMemoryStatus sa raporteze corect o valoare intre 2 si 4 GB, trebuie setata opitunea /LARGEADDRESSAWARE
    • GlobalMemoryStatus nu poate raporta o valoare mai mare sau egala cu 4 GB.
Vezi si
<< Back to Windows API Index

[WinAPI] Cata memorie RAM este in sistem? [2]

Posted: 29 Oct 2009, 10:36
by mesajflaviu
O varianta de aflare a afla cat RAM este in sistem este de a citi in registry valoarea ".Translated" aflata sub cheia
HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory.
Din pacate aceasta valoare este de tipul REG_RESOURCE_LIST care nu este documentat.
Totusi pornind de la ce afiseaza aplicatia Registry Editor si facand analogie cu structuri din DDK cum ar fi CM_RESOURCE_LIST, am ajuns la functia de mai jos:

Code: Select all

DWORD GetTotalPhysicalMemory()
{
   ULONGLONG ulTotalSize = 0;
   const DWORD dwMBFactor = 0x00100000;

   HKEY hKey = NULL;
   LPCTSTR pszSubKey = _T("Hardware\\ResourceMap\\System Resources\\Physical Memory");
   LPCTSTR pszValueName = _T(".Translated");
   
   if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, pszSubKey, &hKey))
   {
      DWORD dwType = 0;
      LPBYTE lpData = NULL;
      DWORD dwLength = 0;
      
      if(ERROR_SUCCESS == RegQueryValueEx(hKey, pszValueName, 0, &dwType, NULL, &dwLength))
      {
         lpData = new BYTE[dwLength];
         RegQueryValueEx(hKey, pszValueName, 0, &dwType, lpData, &dwLength);
         RegCloseKey(hKey);
         
         DWORD dwBusCount = *(DWORD*)lpData;
         _ASSERT(dwBusCount == 1); //  this has to be 1
       
         DWORD dwResourceCount = *(DWORD*)(lpData + 16);
         LPBYTE pmi = lpData + 24;
         
         for(DWORD dwIndex = 0; dwIndex < dwResourceCount; dwIndex++)
         {
            ulTotalSize += *(DWORD*)(pmi + 8);
            pmi += 16;
         }
         delete []lpData;
      }
   }
   return static_cast<DWORD>(ulTotalSize / dwMBFactor);
}

<< Back to Windows API Index