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
|
procedure GetConsoleText(Command:String; var Result:String);
const
LENBUFF = 512; // Augmente le si tu as des problèmes
var
hReadPipe, hWritePipe: THandle;
sa : TSecurityAttributes;
si : TStartupInfo;
pi : TProcessInformation;
lpBuffer : array[0..LENBUFF] of char;
BytesRead : Cardinal;
BytesToRead : integer;
begin
sa.nLength := sizeof( sa );
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := True;
if not CreatePipe( hReadPipe, hWritePipe, @sa, 0 ) then begin
Application.MessageBox( pChar('Error creation Pipe' ), 'Error',IDOK );
exit;
end;
FillChar( si, sizeof(si), 0 );
si.cb := sizeof( si );
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.wShowWindow := SW_HIDE;
si.hStdInput := 0;
si.hStdOutput := hWritePipe;
si.hStdError := hWritePipe;
if not CreateProcess( nil, pChar( Command ), nil, nil, true, 0, nil, nil, si, pi ) then begin
Application.MessageBox( pChar('Error executing command' ), 'Error', IDOK );
CloseHandle( hReadPipe );
CloseHandle( hWritePipe );
exit;
end;
CloseHandle( hWritePipe );
BytesToRead := LENBUFF;
BytesRead := 0;
Result := '';
While (True) do begin
lpBuffer := '';
ReadFile( hReadPipe, lpBuffer, BytesToRead, BytesRead, nil );
if BytesRead=0 then Break;
Result := Result + StrPas(lpBuffer);
end;
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( hReadPipe );
end; |
Partager