View Single Post
  #4  
Old 18-08-2021, 13:09
Cesar82's Avatar
Cesar82 Cesar82 is offline
Registered User
 
Join Date: May 2011
Location: Brazil
Posts: 1,074
Thanks: 1,814
Thanked 2,304 Times in 787 Posts
Cesar82 is on a distinguished road
Quote:
Originally Posted by Masquerade View Post
DiCaPrIo
So I would put if GetSysCores = 6 in the if loop after calling the function?
As the function returns an integer value I think I can use it directly without declaring variables.
if GetSysCores = 6 then
begin

end;

The function can also be simplified using variables of type variant so you can get any supported value just by changing the string you want to search for.
Code:
function GetSysInfo(const WMIClass, WMIProperty: String): Variant;
var
  WbemLocator, WbemServices, WbemObjectSet, WbemObject: Variant;
begin;
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
  WbemObjectSet := WbemServices.ExecQuery('Select ' + WMIProperty + ' from ' + WMIClass);
  if (not VarIsNull(WbemObjectSet)) and (WbemObjectSet.Count > 0) then begin
    WbemObject := WbemObjectSet.ItemIndex(0);
    if not VarIsNull(WbemObject) then
      Result := WbemObject.Properties_.Item(WMIProperty).Value;
  end;
end;

procedure GetFreeMemory(var Free, Total: Extended);
begin
  Total := GetSysInfo('Win32_OperatingSystem', 'TotalVisibleMemorySize');
  Free := GetSysInfo('Win32_OperatingSystem ', 'FreePhysicalMemory');
end;

function CPUCores: Integer;
begin
  Result := GetSysInfo('Win32_Processor', 'NumberOfCores');
end;

function CPUThreads: Integer;
begin
  Result := GetSysInfo('Win32_Processor', 'NumberOfLogicalProcessors');
end;

You can also use the API if you prefer to get Threads.
Code:
type
  TSystemInfo = record
    wProcessorArchitecture: Word;
    wReserved: Word;
    dwPageSize: DWORD;
    lpMinimumApplicationAddress: Integer;
    lpMaximumApplicationAddress: Integer;
    dwActiveProcessorMask: DWORD;
    dwNumberOfProcessors: DWORD;
    dwProcessorType: DWORD;
    dwAllocationGranularity: DWORD;
    wProcessorLevel: Integer;
    wProcessorRevision: Word;
  end;

procedure GetSystemInfo(var lpSystemInfo: TSystemInfo);
  external '[email protected] stdcall delayload';

function GetCPUThreads: Integer;
var
  SysInfo: TSystemInfo;
begin
  GetSystemInfo(SysInfo);
  Result := SysInfo.dwNumberOfProcessors;
end;
Reply With Quote
The Following 3 Users Say Thank You to Cesar82 For This Useful Post:
DiCaPrIo (18-08-2021), ffmla (01-10-2021), Masquerade (18-08-2021)