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
| function TMainForm.GetConsoleText(const szCommande: String): String;
var
sa: TSecurityAttributes;
ReadPipe, WritePipe: THandle;
Start: TStartUpInfo;
ProcessInfo: TProcessInformation;
Buffer: Array[0..4096] of AnsiChar;
BytesRead: DWord;
bLecture: BOOL;
begin
Result := '';
sa.nlength := SizeOf(TSecurityAttributes);
sa.binherithandle := true;
sa.lpsecuritydescriptor := nil;
if Createpipe(ReadPipe, WritePipe, @sa, 0) then
begin
FillChar(Start, Sizeof(Start), #0);
Start.cb := SizeOf(Start);
Start.hStdOutput := WritePipe;
Start.hStdInput := ReadPipe;
Start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
Start.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar('cmd.exe /C ' + szCommande), @sa, @sa, True, NORMAL_PRIORITY_CLASS, nil, nil, Start, ProcessInfo) then
begin
CloseHandle(WritePipe);
try
WaitForSingleObject(ProcessInfo.hProcess, 100);
Application.ProcessMessages;
repeat
BytesRead := 0;
bLecture := ReadFile(ReadPipe, Buffer[0], 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + String(Buffer);
end;
until(not bLecture) or (BytesRead = 0);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
finally
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
end;
end;
end;
end; |
Partager