Get RAM system size

I would like to know how to get the size of my memory through C ++ (on Windows 7).

+6
c ++ windows windows-7 ram
source share
5 answers

Use GetPhysicallyInstalledSystemMemory to get the amount of RAM physically installed on the computer.

(Please note that this requires Windows Vista Service Pack 1 (SP1) or later. This feature is not available in earlier versions of the Windows operating system.)

notes on MSDN say:

The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the SMBIOS computer firmware tables. This may differ from the amount reported by the GlobalMemoryStatusEx function, which sets the ullTotalPhys member of the MEMORYSTATUSEX structure to the amount of physical memory that is available for the operating system to use . The amount of available memory for the operating system may be less than the physical amount of memory installed on the computer, since the BIOS and some drivers can memory as I / O areas for memory-mapped devices, which makes memory inaccessible to the system and applications.

The amount of physical memory received by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the sum, GlobalMemoryStatusEx Function; if it is smaller, the SMBIOS data is garbled and the ERROR_INVALID_DATA function fails. A tidy SMBIOS data may indicate a problem with the user computer.

This means that you would also like to see GlobalMemoryStatusEx .

+7
source share

Ok guys! I found a solution by doing it like this [guru mode]:

 #define _WIN32_WINNT 0x0501 // I misunderstand that #include <windows.h> #include <iostream> #include <cstdlib> using namespace std; int main() { MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); // I misunderstand that GlobalMemoryStatusEx (&statex); cout << "Physical RAM => " << (float)statex.ullTotalPhys/(1024*1024*1024)<< endl; system("PAUSE"); return EXIT_SUCCESS; } 

I needed to define _WIN32_WINNT 0x0501, but I don’t know why [guru mode is disabled].

If someone can explain to me what he does and why he does not work without him.

One more thing that is:

 statex.dwLength = sizeof (statex); 
+5
source share

You want to use GlobalMemoryStatusEx , which returns MEMORYSTATUSEX . The field you want is called ullTotalPhys.

+4
source share

On Windows:

 typedef BOOL (WINAPI *PGMSE)(LPMEMORYSTATUSEX); PGMSE pGMSE = (PGMSE) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), TEXT( "GlobalMemoryStatusEx") ); if ( pGMSE != 0 ) { MEMORYSTATUSEX mi; memset( &mi, 0, sizeof(MEMORYSTATUSEX) ); mi.dwLength = sizeof(MEMORYSTATUSEX); if ( pGMSE( &mi ) == TRUE ) os << "RAM: " << mi.ullTotalPhys / 1048576 << "MB"; else pGMSE = 0; } if ( pGMSE == 0 ) { MEMORYSTATUS mi; memset( &mi, 0, sizeof(MEMORYSTATUS) ); mi.dwLength = sizeof(MEMORYSTATUS); GlobalMemoryStatus( &mi ); os << "RAM: " << mi.dwTotalPhys / 1048576 << "MB"; } 

On Linux:

Read /proc/meminfo .

+3
source share

0x501 is a version of WindowsXP, i.e. The MEMORYSTATUSEX not supported by some older versions of Windows. Probably your windef.h points to a lower WINVER than 0x5XX .

0
source share

All Articles