There are several ways to monitor CPU and RAM usage in Delphi, one of the most common ways is to use the Windows Management Interface (WMI) to retrieve information about the system’s performance.
Here is an example of how to use WMI in Delphi to monitor CPU and RAM usage:
- Declare the
IWbemServices
andIWbemLocator
interfaces in the implementation section of your unit:
1 2 3 4 5 6 |
uses ActiveX, Variants, ComObj; var WbemLocator: IWbemLocator; WbemServices: IWbemServices; |
- Initialize the
WbemLocator
andWbemServices
objects in the form’sOnCreate
event:
123456procedure TForm1.FormCreate(Sender: TObject);beginCoInitialize(nil);WbemLocator := CreateOleObject(‘WbemScripting.SWbemLocator’) as IWbemLocator;WbemServices := WbemLocator.ConnectServer(‘.’, ‘root\CIMV2’, ”, ”, ”, ”, 0, nil);end;
This connects to the local WMI namespace (indicated by the ‘.’) and the ‘root\CIMV2’ namespace, which is the default namespace for performance counters on Windows. - Use the
ExecQuery
method of theWbemServices
object to query for the CPU and RAM usage:
1234567891011121314151617function TForm1.GetCPUUsage: Integer;varobjWMIService: OLEVariant;colItems: OLEVariant;colItem: OLEVariant;oEnum: IEnumvariant;iValue: LongWord;beginobjWMIService := GetObject(‘winmgmts:\\localhost\root\CIMV2’);colItems := objWMIService.ExecQuery(‘SELECT * FROM Win32_Processor’, ‘WQL’, 0);oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;if oEnum.Next(1, colItem, nil) = 0 thenbeginiValue := colItem.LoadPercentage;Result := iValue;end;end;
123456789101112131415161718function TForm1.GetRAMUsage: Cardinal;varobjWMIService: OLEVariant;colItems: OLEVariant;colItem: OLEVariant;oEnum: IEnumvariant;FreeMemory, TotalMemory: Cardinal;beginobjWMIService := GetObject(‘winmgmts:\\localhost\root\CIMV2’);colItems := objWMIService.ExecQuery(‘SELECT * FROM Win32_OperatingSystem’, ‘WQL’, 0);oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;if oEnum.Next(1, colItem, nil) = 0 thenbeginFreeMemory := colItem.FreePhysicalMemory;TotalMemory := colItem.TotalVisibleMemorySize;Result := (TotalMemory – FreeMemory)end;end; - Use the
GetCPUUsage
andGetRAMUsage
functions to retrieve the current CPU and RAM usage:
12345procedure TForm1.UpdateUsage;beginCPULabel.Caption := IntToStr(GetCPUUsage) + ‘%’;RAMLabel.Caption := IntToStr(GetRAMUsage div 1024) + ‘MB’;end;
In this example, we are displaying the usage as a percentage for CPU and in MegaBytes for RAM - To update the usage regularly, you can use a timer to call the
UpdateUsage
method at a certain interval.
12345678910procedure TForm1.FormCreate(Sender: TObject);beginTimer1.Interval := 1000;Timer1.Enabled := True;end;procedure TForm1.Timer1Timer(Sender: TObject);beginUpdateUsage;end;
This will update the usage every 1000ms (1s). - Make sure to release the resources when you are done using the WMI:
123456procedure TForm1.FormDestroy(Sender: TObject);beginWbemServices := nil;WbemLocator := nil;CoUninitialize;end;
It is important to note that this is just an example of how to use WMI in Delphi to monitor CPU and RAM usage, and it could be improved with error checking and handling, and also there are other ways to monitor the system performance like using the
GetPerformanceInfo
function in the Windows API, or by reading the values of certain performance counters directly.
Leave a Reply