Delphi 2007:GlobalMemoryStatus获取总RAM [英] Delphi 2007: GlobalMemoryStatus to get total RAM
问题描述
我需要使用Delphi 2007获取系统的总物理内存.在具有4GB或更大容量的系统上使用GlobalMemoryStatus似乎会给我带来错误.在Delphi 2007中,GlobalMemoryStatusEx不存在,因此我将函数调用手动添加到程序中.它可以正确返回Windows 7 x64上的内存(8GB),但是在Vista x32系统上,它仍返回不正确的值(在该系统上应为4GB,但返回2.9GB).知道我可能做错了什么吗?GlobalMemoryStatusEx可以在较旧的操作系统上运行吗?
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;
推荐答案
这是可以预期的,您没有做错任何事情.Windows将不会在32位操作系统上报告4GB内存.这是来自MSDN的报价
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":
由于很久以前就做出了架构决定,如果您有4GB的存储空间,安装了物理RAM,Windows只能报告一部分物理4GB RAM(范围从〜2.75GB到3.5GB,具体取决于安装的设备,主板的芯片组和BIOS).
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
应该可以在Windows 2000及更高版本上运行(较新的MSDN文档不包括Win2K,但较早的文档具有Win2K).
GlobaMemoryStatusEx
should work from Windows 2000 and on (later MSDN documents exclude Win2K but earlier ones had it).
这篇关于Delphi 2007:GlobalMemoryStatus获取总RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!