4
votes

I need to get the total physical memory of a system using Delphi 2007. Using GlobalMemoryStatus on a system with 4GB or greater seems to give me errors. In Delphi 2007 GlobalMemoryStatusEx does not exist so I added the function call to my program manually. It returns the memory on my Windows 7 x64 correctly (8GB) but on a Vista x32 system it still returns an incorrect value (should be 4GB on this system but returns 2.9GB). Any Idea what I may be doing wrong? And will the GlobalMemoryStatusEx work on older operating systems?

type
  DWORDLONG = UInt64;

  PMemoryStatusEx = ^TMemoryStatusEx;
  TMemoryStatusEx = packed record
    dwLength: DWORD;
    dwMemoryLoad: DWORD;
    ullTotalPhys: DWORDLONG;
    ullAvailPhys: DWORDLONG;
    ullTotalPageFile: DWORDLONG;
    ullAvailPageFile: DWORDLONG;
    ullTotalVirtual: DWORDLONG;
    ullAvailVirtual: DWORDLONG;
    ullAvailExtendedVirtual: DWORDLONG;
  end;

function GlobalMemoryStatusEx(var lpBuffer: TMemoryStatusEx): BOOL; stdcall; external kernel32;

function getmemorysize:word;
var
  memory: TMemoryStatusEx;
begin
  FillChar(memory, SizeOf(memory), 0);
  memory.dwLength := SizeOf(memory);
  GlobalMemoryStatusEx(memory);
  result:=memory.ullTotalPhys div (1024*1024);
end;
1
@SertacAkyuz That answers my Vista question, but do you know if the above code will work on older operating systems like Windows XP? I am just a little concerned since I had to manually add the prototypes for GlobalMemoryStatusEx. Thanks.Evan Zimmerman
Ace - The declaration is probably missing because of Win9x support of the IDE, don't worry. @Ken - Done, and thanks.Sertac Akyuz

1 Answers

3
votes

This is to be expected, you're not doing anything wrong. Windows will not report 4GB ram on a 32bit OS. Here's a quote from an MSDN blog article entitled "The 3GB-not-4GB RAM problem":

Due to an architectural decision made long ago, if you have 4GB of physical RAM installed, Windows is only able to report a portion of the physical 4GB of RAM (ranges from ~2.75GB to 3.5GB depending on the devices installed, motherboard's chipset & BIOS).

GlobaMemoryStatusEx should work from Windows 2000 and on (later MSDN documents exclude Win2K but earlier ones had it).