How can I get information about other processes using Delphi?

I want to make a task manager program that displays this information:

  • Image title
  • Memory usage
  • PID

How can i do this?

+3
source share
5 answers

You don't need J (WS) CL, so there is a simple WinAPI call that does almost everything you need, and that is CreateToolhelp32Snapshot. To get a snapshot of all running processes, you must call it as follows:

var
  snapshot: THandle;
begin
  snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 

. Process32First Process32Next, - PROCESSENTRY32 - ( , , ).

uses 
  Windows, TLHelp32, SysUtils; 

var 
  snapshot: THandle; 
  ProcEntry: TProcessEntry32; 
  s: String; 
begin
  snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
  if (snapshot <> INVALID_HANDLE_VALUE) then begin 
    ProcEntry.dwSize := SizeOf(ProcessEntry32); 
    if (Process32First(snapshot, ProcEntry)) then begin 
      s := ProcEntry.szExeFile; 
      // s contains image name of the first process 
      while Process32Next(snapshot, ProcEntry) do begin 
        s := ProcEntry.szExeFile; 
        // s contains image name of the current process 
      end; 
    end; 
  end; 
  CloseHandle(snapshot);

, , , API, GetProcessMemoryInfo

uses
  psAPI;

var
  pmc: TProcessMemoryCounters;
begin
  pmc.cb := SizeOf(pmc) ;
  if GetProcessMemoryInfo(processID, @pmc, SizeOf(pmc)) then
    // Usage in Bytes: pmc.WorkingSetSize
  else
    // fail

, .

+7

PSAPI (Process Status API). JCL Delphi PSAPI.

stackoverflow Delphi PSAPI, .

-

+2

WMI Win32_Process, . , Win32_PerfFormattedData_PerfProc_Process, , .

program WMIProcessInfo;

{$APPTYPE CONSOLE}

uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;


procedure GetWin32_Process;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  User          : OLEVariant;
  Domain        : OLEVariant;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
      Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_Process','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;

      WriteLn(Format('%-20s %6s %10s %10s %10s',['Caption','PID','User','Domain','Working Set ( Kb Memory)']));
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
      colItem.GetOwner(User,Domain);
      if colItem.GetOwner( User, Domain ) =0 then //get the user and domain
      WriteLn(Format('%-20s %6s %10s %10s %10s',[colItem.Caption,colItem.ProcessId,User,Domain,colItem.WorkingSetSize / 1024]))
      else
      WriteLn(Format('%-20s %6s %10s %10s %10s',[colItem.Caption,colItem.ProcessId,'','',colItem.WorkingSetSize / 1024]));

  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_Process;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.
+2

Jwscl , (JwsclTerminalServer):

 var
   ATerminalServer: TJwTerminalServer;
   i: Integer;
 begin
   // Create Terminal Server instance and allocate memory for it
   ATerminalServer := TjwTerminalServer.Create;

   // Set servername (only in case of remote server)
   ATerminalServer.Server := 'TS001';

   // Remember that EnumerateProcesses will automatically connect to the
   // Terminal Server for you. The connect function raises an Exception
   // if the connection attempt was unsuccessfull, so better use try..except
   try
     if ATerminalServer.EnumerateProcesses then
     begin

       // Now loop through the list
       for i := 0 to ATerminalServer.Processes.Count - 1 do
       begin
         Memo1.Lines.Add(ATerminalServer.Processes[i].ProcessName);
       end;

     end;
   except
     on E: EJwsclWinCallFailedException do
     begin
       // Handle Exception here
     end;
   end;

   // Free Memory
   ATerminalServer.Free;
 end;

Terminal Server, , , .

.

ProcessMemUsage ProcessVirtualSize, Pid ProcessId

+1
source

ProcessInfo contains basic information about running processes on Windows. It is open source and contains a demo of the task manager.

+1
source

Source: https://habr.com/ru/post/1740632/


All Articles