Query available RAM in Inno Setup

南楼画角 提交于 2020-01-01 19:31:16

问题


I need to get the available RAM to determinate some characteristics to my software.

I have this code to show my PC's RAM:

type
  DWORDLONG = Int64;
  TMemoryStatusEx = 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;
  external 'GlobalMemoryStatusEx@kernel32.dll stdcall';

function InitializeSetup: Boolean;
var
  MemoryStatus: TMemoryStatusEx;
  RAM: String;
begin
  Result := True;
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  if GlobalMemoryStatusEx(MemoryStatus) then
  begin
    RAM := Int64ToStr(MemoryStatus.ullTotalPhys/1000000000);
    MsgBox('This PC has '+RAM+' GB of RAM', mbInformation, MB_OK);
  end;
end;

Based on Inno Setup - How can I check system specs before/during installation?


回答1:


If you already have the code for GlobalMemoryStatusEx from Inno Setup - How can I check system specs before/during installation?, just use the ullAvailPhys field.


Another possibility is using WMI query:

var
  Query: string;
  WbemLocator, WbemServices, WbemObjectSet: Variant;
  OperatingSystem: Variant;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
  Query := 'SELECT FreePhysicalMemory FROM Win32_OperatingSystem';
  WbemObjectSet := WbemServices.ExecQuery(Query);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    OperatingSystem := WbemObjectSet.ItemIndex(0);
    Log(Format('Free physical memory = %d GB', [
      Integer(OperatingSystem.FreePhysicalMemory div (1024*1024))]));
  end;
end;

See also Is there a way to read the system's information in Inno Setup (shows, among other, how to retrieve total physical memory using WMI query).




回答2:


Read ullAvailPhys field:

type
  DWORDLONG = Int64;
  TMemoryStatusEx = 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;
  external 'GlobalMemoryStatusEx@kernel32.dll stdcall';

function InitializeSetup: Boolean;
var
  MemoryStatus: TMemoryStatusEx;
  RAM, Available: String;
begin
  Result := True;
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  { if the GlobalMemoryStatusEx function call succeed, then... }
  if GlobalMemoryStatusEx(MemoryStatus) then
  begin
    RAM := Int64ToStr(MemoryStatus.ullTotalPhys/1000000000);
    Available := Int64ToStr(MemoryStatus.ullAvailPhys/1000000000)
    MsgBox('This PC has '+RAM+' GB of RAM, Available ' + Available, mbInformation, MB_OK);
  end;
end;


来源:https://stackoverflow.com/questions/44035181/query-available-ram-in-inno-setup

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