unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, PsAPI;

type
  TfMain = class(TForm)
    btnGetList: TButton;
    lbProcList: TListBox;
    procedure btnGetListClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

procedure TfMain.btnGetListClick(Sender: TObject);
var
  PrIDs : array[0..$FFF] of dword; // Масив для зберігання ідентифікаторів процесів
  Cnt, ModCnt : cardinal;
  ProcCnt, I  : word;
  hProcess, hMod : THandle;
  ModuleName  : array[0..MAX_PATH] of char;
begin
  lbProcList.Items.Clear;

  EnumProcesses(@PrIDs, SizeOf(PrIDs), Cnt);

  ProcCnt := Cnt div SizeOf(DWORD);
  for I := 0 to ProcCnt - 1 do begin
    case PrIDs[I] of
      0 : lbProcList.Items.Add('Процес Idle (недіяння системи)');
      4 : lbProcList.Items.Add('Процес System');
      else begin
        hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PrIDs[I]);
        if hProcess <> 0 then begin
          EnumProcessModules(hProcess, @hMod, SizeOf(hMod), ModCnt);
          GetModuleFilenameEx(hProcess, hMod, ModuleName, SizeOf(ModuleName));
          lbProcList.Items.Add(ModuleName);
          CloseHandle(hProcess);
        end else lbProcList.Items.Add('-= Назву отримати неможливо =-');
      end;
    end;
  end;
end;

end.
