1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| procedure TForm1.FormCreate(Sender: TObject);
//Dans uses ajouter : process
const
READ_BYTES = 2048;
var
S: TStringList;
M: TMemoryStream;
P: TProcess;
n: LongInt;
BytesRead: LongInt;
i: integer;
sLine: string;
begin
M := TMemoryStream.Create;
BytesRead := 0;
P := TProcess.Create(nil);
// CommandLine différente selon l'OS
{$IFDEF WINDOWS}
P.CommandLine := 'tasklist';
{$ELSE}
//Pour Linux et MAC OS X
// P.CommandLine := '???';
{$ENDIF}
P.ShowWindow:= swoHIDE;
P.Options := [poUsePipes];
P.Execute;
while P.Running do begin
M.SetSize(BytesRead + READ_BYTES);
n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
if n > 0 then begin
Inc(BytesRead, n);
end else
Sleep(100);
end;
repeat
M.SetSize(BytesRead + READ_BYTES);
n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
if n > 0 then begin
Inc(BytesRead, n);
end;
until n <= 0;
M.SetSize(BytesRead);
S := TStringList.Create;
S.LoadFromStream(M);
//Eventuellement ajouter un Memo sur la Form pour voir le résultat
//Memo1.Clear;
//Memo1.Lines.AddStrings(S);
if S.count>0 then begin
for i:=0 to S.count -1 do begin
sLine:=trim(S[i]);
//Traiter chaque sLine ici pour récupérer le nom.exe contenu
//Voir petite RegEx ci-dessous
end;
end;
S.Free;
P.Free;
M.Free;
end; |
Partager