8

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

0

5 Answers 5

9

Use GetPhysicallyInstalledSystemMemory to retrieve the amount of RAM that is physically installed on the computer.

(Note that this requires Windows Vista SP1 or later. The function is not available on earlier versions of the Windows operating system.)

The remarks on MSDN say:

The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. This can 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 memory available to the operating system can be less than the amount of memory physically installed in the computer because the BIOS and some drivers may reserve memory as I/O regions for memory-mapped devices, making the memory unavailable to the operating system and applications.

The amount of physical memory retrieved by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the amount reported by the GlobalMemoryStatusEx function; if it is less, the SMBIOS data is malformed and the function fails with ERROR_INVALID_DATA. Malformed SMBIOS data may indicate a problem with the user's computer.

That means, you would also want to look at GlobalMemoryStatusEx.

0
7

Okay, guys! I've found the solution by doing this like that [guru mode on]:

#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 had to define _WIN32_WINNT 0x0501, but i don't know why [guru mode is off].

If somebody could explain me what it is doing and why it doesn't work without it.

One more thing, what is that:

statex.dwLength = sizeof (statex);
1
5

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.

4

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

1

The 0x501 is the WindowsXP version, i.e. the MEMORYSTATUSEX struct is not supported by some older Windows versions. Your windef.h probably points to a lower WINVER than 0x5XX.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.