Conversion of Gant C ++ code, to console application in Delphi:
program MemoryProcessCMD;
{* Based in Gant(https://stackoverflow.com/users/12460/gant) code*}
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
psapi,
Windows;
procedure PrintMemoryInfo(processID: DWORD);
var
hProcess: THandle;
pmc: PROCESS_MEMORY_COUNTERS;
total: DWORD;
begin
// Print the process identifier.
Writeln(format('Process ID: %d', [processID]));
// Print information about the memory usage of the process.
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE,
processID);
if (hProcess = 0) then
begin
exit;
end;
if (GetProcessMemoryInfo(hProcess, @pmc, SizeOf(pmc))) then
begin
Writeln(format(#09'PageFaultCount: 0x%.8X', [pmc.PageFaultCount]));
Writeln(format(#09'PeakWorkingSetSize: 0x%.8X', [pmc.PeakWorkingSetSize]));
Writeln(format(#09'WorkingSetSize: 0x%.8X', [pmc.WorkingSetSize]));
Writeln(format(#09'QuotaPeakPagedPoolUsage: 0x%.8X',
[pmc.QuotaPeakPagedPoolUsage]));
Writeln(format(#09'QuotaPagedPoolUsage: 0x%.8X',
[pmc.QuotaPagedPoolUsage]));
Writeln(format(#09'QuotaPeakNonPagedPoolUsage: 0x%.8X',
[pmc.QuotaPeakNonPagedPoolUsage]));
Writeln(format(#09'QuotaNonPagedPoolUsage: 0x%.8X',
[pmc.QuotaNonPagedPoolUsage]));
Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
Writeln(format(#09'PeakPagefileUsage: 0x%.8X', [pmc.PeakPagefileUsage]));
Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
end;
CloseHandle(hProcess);
end;
var
aProcesses: array [0 .. 1024] of DWORD;
cbNeeded, cProcesses: DWORD;
i: Integer;
begin
try
// Get the list of process identifiers.
if (not EnumProcesses(@aProcesses, SizeOf(aProcesses), &cbNeeded)) then
halt(1);
// Calculate how many process identifiers were returned.
cProcesses := cbNeeded div SizeOf(DWORD);
// Print the memory usage for each process
for i := 0 to cProcesses - 1 do
begin
PrintMemoryInfo(aProcesses[i]);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.