Skip to content

Instantly share code, notes, and snippets.

@AMZN-alexpete
Created September 1, 2023 21:02
Show Gist options
  • Select an option

  • Save AMZN-alexpete/8ab0cb06d260d8a2bbd9b6f35b00236c to your computer and use it in GitHub Desktop.

Select an option

Save AMZN-alexpete/8ab0cb06d260d8a2bbd9b6f35b00236c to your computer and use it in GitHub Desktop.
Retrieve physically installed system memory (Windows)
m_value = 0;
HMODULE kernel32Handle = ::GetModuleHandleW(L"Kernel32.dll");
if (kernel32Handle)
{
using Func = BOOL(WINAPI*)(_Out_ PULONGLONG TotalMemoryInKilobytes);
auto getPhysicallyInstalledSystemMemoryFunc =
reinterpret_cast<Func>(::GetProcAddress(kernel32Handle, "GetPhysicallyInstalledSystemMemory"));
if (getPhysicallyInstalledSystemMemoryFunc)
{
// Physical RAM is returned in KB
constexpr float div = 1024.0 * 1024.0;
ULONGLONG ullTotalPhys = 0;
if (getPhysicallyInstalledSystemMemoryFunc(&ullTotalPhys))
{
m_value = aznumeric_cast<float>(ullTotalPhys) / div;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment