I am trying to get the total installed memory. I have 6GB installed but this is returning 5.47GB. What can I do to fix this? I did a build on a x64 PC and am running the app on a x64 PC.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint)Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX));
}
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern Boolean GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
public static String GetTotalRam
{
get
{
ulong installedMemory = 0;
NativeMethods.MEMORYSTATUSEX memStatus = new NativeMethods.MEMORYSTATUSEX();
if (NativeMethods.GlobalMemoryStatusEx(memStatus))
{
installedMemory = memStatus.ullTotalPhys;
}
return ConvertBytes(installedMemory);
}
}