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
| procedure run(cmd:string; txt:TStrings);
var
PipeIn :THandle;
PipeOut :THandle;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
Buffer: array[0..4096] of Char;
NbRead: DWord;
begin
// Créer un pipe d'entrées/sorties
CreatePipe(PipeIn,PipeOut,nil,0);
// Créer le process en redirigeant les E/S
FillChar(StartupInfo, SizeOf(TStartupInfo),0);
StartupInfo.cb :=SizeOf(TStartupInfo);
StartupInfo.dwFlags :=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow:=SW_HIDE;
StartupInfo.hStdInput :=PipeIn;
StartupInfo.hStdOutput :=PipeOut;
StartupInfo.hStdError :=PipeOut;
CreateProcess(nil,PChar(cmd),nil,nil,True,0,nil,nil,StartupInfo,ProcessInfo);
// attendre la fin du process
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
// Fermer la sortie
CloseHandle(PipeOut);
// pour relire le tout
while ReadFile(PipeIn,Buffer,4096,NbRead,nil) do txt.Add(Buffer);
// fin
CloseHandle(PipeIn);
end; |
Partager