AceInfinity
Emeritus, Contributor
Here's a snippet I put together for retrieving the total amount of physically installed memory. Should be the same value that you see in the System Information window.
Supported by Vista SP1 and greater, for anything less you could implement a fallback to GlobalMemoryStatusEx(), and retrieve the value from there. That function also provides a few more values regarding system memory, so if you're looking to get more than just the physically installed memory, you might just want to use that function instead since it is supported by XP and greater. GetPhysicallyInstalledSystemMemory() is the newer API however.
:grin1:
Code:
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#define GPA(module, func) GetProcAddress(GetModuleHandle(module), func)
typedef BOOL (WINAPI *PGetPhysicallyInstalledSystemMemory)(PULONGLONG TotalMemoryInKilobytes);
/* description: retrieves installed physical memory (GB)
* returns: TRUE on success, FALSE on failure */
BOOL installed_physical_memory(double *result)
{
ULONGLONG mem; /* physical memory installed (kb) */
PGetPhysicallyInstalledSystemMemory
pFGetPhysicallyInstalledSystemMemory = GPA("kernel32.dll", "GetPhysicallyInstalledSystemMemory");
if (!result) return FALSE;
if (!pFGetPhysicallyInstalledSystemMemory
|| !pFGetPhysicallyInstalledSystemMemory(&mem))
{
return FALSE;
}
*result = mem / 1024.0 / 1024.0;
return TRUE;
}
int main(void)
{
double physical_mem;
if (!installed_physical_memory(&physical_mem))
{
fputs("An error ocurred\n", stderr);
exit(1);
}
printf("Installed Physical RAM: %.2f GB\n", physical_mem);
exit(0);
}
Supported by Vista SP1 and greater, for anything less you could implement a fallback to GlobalMemoryStatusEx(), and retrieve the value from there. That function also provides a few more values regarding system memory, so if you're looking to get more than just the physically installed memory, you might just want to use that function instead since it is supported by XP and greater. GetPhysicallyInstalledSystemMemory() is the newer API however.
:grin1:
Last edited: