unit meminfo;
interface
uses
Windows, SysUtils, Classes;
function getavailpagefile: Int64;
function gettotalpagefile: Int64;
function getavailphysmemory: Int64;
function gettotalphysmemory: Int64;
implementation
type
PMemoryStatusEx = ^TMemoryStatusEx;
LPMEMORYSTATUSEX = PMemoryStatusEx;
{$EXTERNALSYM LPMEMORYSTATUSEX}
_MEMORYSTATUSEX =
packed Record
dwLength : DWORD;
dwMemoryLoad : DWORD;
ullTotalPhys : Int64;
ullAvailPhys : Int64;
ullTotalPageFile: Int64;
ullAvailPageFile: Int64;
ullTotalVirtual : Int64;
ullAvailVirtual : Int64;
ullAvailExtenededVirtual : Int64;
end;
{$EXTERNALSYM _MEMORYSTATUSEX}
TMemoryStatusEx = _MEMORYSTATUSEX;
MEMORYSTATUSEX = _MEMORYSTATUSEX;
{$EXTERNALSYM MEMORYSTATUSEX}
function GlobalMemoryStatusEx(
var lpBuffer: TMemoryStatusEx): BOOL;
stdcall;
type
TFNGlobalMemoryStatusEx =
function(
var msx: TMemoryStatusEx): BOOL;
stdcall;
var
FNGlobalMemoryStatusEx: TFNGlobalMemoryStatusEx;
begin
lpBuffer.dwLength := SizeOf(TMemoryStatusEx);
FNGlobalMemoryStatusEx := TFNGlobalMemoryStatusEx(
GetProcAddress(GetModuleHandle(kernel32), '
GlobalMemoryStatusEx'));
if not Assigned(FNGlobalMemoryStatusEx)
then
begin
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
Result := False;
end
else
begin
Result := FNGlobalMemoryStatusEx(lpBuffer);
end;
end;
function getIsWin2kXP: Boolean;
var
oviVersionInfo: TOSVERSIONINFO;
begin
oviVersionInfo.dwOSVersionInfoSize := SizeOf(oviVersionInfo);
if not GetVersionEx(oviVersionInfo)
then raise
Exception.Create('
Can''
t get the Windows version');
if (oviVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT)
and
(oviVersionInfo.dwMajorVersion >= 5)
then getIsWin2kXP := true
else getIsWin2kXP := false;
end;
function gettotalphysmemory: Int64;
var
memory: TMemoryStatus;
memoryEx: TMemoryStatusEx;
begin
if getIsWin2kXP
then
begin
memoryEx.dwLength := sizeof(TMemoryStatusEx);
if GlobalMemoryStatusEx(memoryEx)
then gettotalphysmemory :=
memoryEx.ulltotalphys
else gettotalphysmemory := 0;
end
else
begin
memory.dwLength := sizeof(TMemoryStatus);
GlobalMemoryStatus(memory);
gettotalphysmemory := memory.dwtotalphys;
end;
end;
function getavailphysmemory: Int64;
var
memory: TMemoryStatus;
memoryEx: TMemoryStatusEx;
begin
if getIsWin2kXP
then
begin
memoryEx.dwLength := sizeof(TMemoryStatusEx);
if GlobalMemoryStatusEx(memoryEx)
then getavailphysmemory :=
memoryEx.ullavailphys
else getavailphysmemory := 0;
end
else
begin
memory.dwLength := sizeof(TMemoryStatus);
GlobalMemoryStatus(memory);
getavailphysmemory := memory.dwavailphys;
end;
end;
function gettotalpagefile: Int64;
var
memory: TMemoryStatus;
memoryEx: TMemoryStatusEx;
begin
if getIsWin2kXP
then
begin
memoryEx.dwLength := sizeof(TMemoryStatusEx);
if GlobalMemoryStatusEx(memoryEx)
then gettotalpagefile :=
memoryEx.ulltotalpagefile
else gettotalpagefile := 0;
end
else
begin
memory.dwLength := sizeof(TMemoryStatus);
GlobalMemoryStatus(memory);
gettotalpagefile := memory.dwtotalpagefile;
end;
end;
function getavailpagefile: Int64;
var
memory: TMemoryStatus;
memoryEx: TMemoryStatusEx;
begin
if getIsWin2kXP
then
begin
memoryEx.dwLength := sizeof(TMemoryStatusEx);
if GlobalMemoryStatusEx(memoryEx)
then getavailpagefile :=
memoryEx.ullavailpagefile
else getavailpagefile := 0;
end
else
begin
memory.dwLength := sizeof(TMemoryStatus);
GlobalMemoryStatus(memory);
getavailpagefile := memory.dwavailpagefile;
end;
end;