我正在使用DelphiIXE。
我了解到GlobalMemoryStatus可能会在64台以上的计算机上返回错误的结果 4G RAM,因此应该使用GlobalMemoryStatusex。
但是,另一方面,如果我在32台计算机上使用GlobalMemoryStatusex,结果也是错误的(返回的数字是0或者很大)。
当然我可以准备两个程序:一个用于64个,一个用于32个计算机, 并使用正确的内存状态,但有没有办法使用相同的 打电话或认出电脑是64? 做一些像:
if comp64 then begin
GlobalMemoryStatusex
....
end
else begin
GlobalMemoryStatus
....
end;
这是我现在使用的代码:
var
MS1: TMemoryStatusex;
begin
GlobalMemoryStatusex(MS1);
showmessage('KiloBytes of physical memory: '+FormatFloat('#,###" KB"', MS1.ullTotalPhys / 1024)+chr(10)+
'Percent of memory in use: '+Format('%d%%', [MS1.dwMemoryLoad])+chr(10)+
'KiloBytes of free physical memory: '+FormatFloat('#,###" KB"', MS1.ullAvailPhys /1024)+chr(10)+chr(10)+
'KiloBytes of paging file space: '+FormatFloat('#,###" KB"', MS1.ullTotalPageFile / 1024)+chr(10)+
'KiloBytes of free paging file space: '+FormatFloat('#,###" KB"', MS1.ullAvailPageFile / 1024)+chr(10)+chr(10)+
'KiloBytes of virtual address space: '+FormatFloat('#,###" KB"', MS1.ullTotalVirtual / 1024)+chr(10)+
'KiloBytes of free virtual address space: '+FormatFloat('#,###" KB"', MS1.ullAvailVirtual / 1024) );
提前致谢。
TOndrej,这是代码:
var MS1:TMemoryStatusex;
GlobalMemoryStatusex(MS1);
showmessage('KiloBytes of physical memory: '+FormatFloat('#,###" KB"', MS1.ullTotalPhys / 1024)+chr(10)+
'Percent of memory in use: '+Format('%d%%', [MS1.dwMemoryLoad])+chr(10)+
'KiloBytes of free physical memory: '+FormatFloat('#,###" KB"', MS1.ullAvailPhys /1024)+chr(10)+chr(10)+
'KiloBytes of paging file space: '+FormatFloat('#,###" KB"', MS1.ullTotalPageFile / 1024)+chr(10)+
'KiloBytes of free paging file space: '+FormatFloat('#,###" KB"', MS1.ullAvailPageFile / 1024)+chr(10)+chr(10)+
'KiloBytes of virtual address space: '+FormatFloat('#,###" KB"', MS1.ullTotalVirtual / 1024)+chr(10)+
'KiloBytes of free virtual address space: '+FormatFloat('#,###" KB"', MS1.ullAvailVirtual / 1024) );
答案 0 :(得分:4)
您似乎没有初始化结构,也没有检查返回代码。这是一个应该可行的可编辑项目:
program Project1;
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
SysUtils;
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;
procedure Main;
var
MemStatus: TMemoryStatusEx;
begin
// initialize the structure
FillChar(MemStatus, SizeOf(MemStatus), 0);
MemStatus.dwLength := SizeOf(MemStatus);
// check return code for errors
Win32Check(GlobalMemoryStatusEx(MemStatus));
Writeln(Format('dwLength: %d', [MemStatus.dwLength]));
Writeln(Format('dwMemoryLoad: %d', [MemStatus.dwMemoryLoad]));
Writeln(Format('ullTotalPhys: %d', [MemStatus.ullTotalPhys]));
Writeln(Format('ullAvailPhys: %d', [MemStatus.ullAvailPhys]));
Writeln(Format('ullTotalPageFile: %d', [MemStatus.ullTotalPageFile]));
Writeln(Format('ullAvailPageFile: %d', [MemStatus.ullAvailPageFile]));
Writeln(Format('ullTotalVirtual: %d', [MemStatus.ullTotalVirtual]));
Writeln(Format('ullAvailVirtual: %d', [MemStatus.ullAvailVirtual]));
Writeln(Format('ullAvailExtendedVirtual: %d', [MemStatus.ullAvailExtendedVirtual]));
end;
begin
try
Main;
except
on E: Exception do
begin
ExitCode := 1;
Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
end;
end;
end.
答案 1 :(得分:-2)
我想我找到了答案:
if sizeof(anypointervariable)>4 then 64bit else 32bit;