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
| procedure CaptureConsoleOutput(const Cmd, Paramt: String; Console: TMemo);
const
CReadBuffer = 2400;
var
Security: TSecurityAttributes;
hRead, hWrite : THandle;
WinCmd: TStartupInfo;
piProcess: TProcessInformation;
pBuffer: array[0..CReadBuffer] of AnsiChar;
dRead, dRunning : DWord;
begin
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := True;
Security.lpSecurityDescriptor := nil;
if CreatePipe(hRead, hWrite, @Security, 0) then
begin
FillChar(WinCmd, SizeOf(TStartupInfo), #0);
with WinCmd do
begin
cb := SizeOf(TStartupInfo);
hStdInput := hRead;
hStdOutput := hWrite;
hStdError := hWrite;
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_Hide;
end;
if CreateProcess(nil, PChar(Cmd + ' ' + Paramt), @Security, @Security, True, NORMAL_PRIORITY_CLASS, nil, nil, WinCmd, piProcess) then
begin
repeat
repeat
dRunning := WaitForSingleObject(piProcess.hProcess, 100);
Application.ProcessMessages;
dRead := 0;
ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);
pBuffer[dRead] := #0;
OemToAnsi(pBuffer, pBuffer);
Console.Lines.Add(String(pBuffer));
until (dRead <> CReadBuffer);
until (dRunning <> WAIT_TIMEOUT);
CloseHandle(piProcess.hProcess);
CloseHandle(piProcess.hThread);
end;
CloseHandle(hRead);
CloseHandle(hWrite);
end;
end; |
Partager