#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);
}