Delphi 2007: GlobalMemoryStatus to get total RAM

2019-06-27 11:18发布

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条回答
迷人小祖宗
2楼-- · 2019-06-27 11:30

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).

查看更多
登录 后发表回答