Delphi 2007: GlobalMemoryStatus to get total RAM

女生的网名这么多〃 提交于 2019-12-10 15:39:43

问题


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:


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



来源:https://stackoverflow.com/questions/19779234/delphi-2007-globalmemorystatus-to-get-total-ram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!