unit IPCConsole;
interface
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
SecurityAttr: TSecurityAttributes;
PipeOutputRead: THandle;
PipeOutputWrite: THandle;
PipeErrorsRead: THandle;
PipeErrorsWrite: THandle;
ConsoleOutput : TStringList;
ConsoleErrors : TStringList;
function StartProcessDone(
const AProcessStartCommand:
String): Boolean;
function SendCommandDone(
const Command:
String;
var Output, Errors: TStringList): Boolean;
implementation
function StartProcessDone(
const AProcessStartCommand:
String): Boolean;
var
Succeed: Boolean;
Buffer:
array [0..255]
of Char;
NumberOfBytesRead: DWORD;
Stream: TMemoryStream;
begin
//Initialisierung ProcessInfo
FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
//Initialisierung SecurityAttr
FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
SecurityAttr.nLength := SizeOf(SecurityAttr);
SecurityAttr.bInheritHandle := true;
SecurityAttr.lpSecurityDescriptor :=
nil;
//Pipes erzeugen
CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0);
CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0);
//Initialisierung StartupInfo
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.hStdInput := 0;
StartupInfo.hStdOutput := PipeOutputWrite;
StartupInfo.hStdError := PipeErrorsWrite;
StartupInfo.wShowWindow := sw_Hide;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW
or STARTF_USESTDHANDLES;
if CreateProcess(
nil, PChar(command),
nil,
nil, true,
CREATE_DEFAULT_ERROR_MODE
or CREATE_NEW_CONSOLE
or NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo, ProcessInfo)
then begin
result:=true;
//Write-Pipes schließen
//CloseHandle(PipeOutputWrite);
//CloseHandle(PipeErrorsWrite);
//Ausgabe Read-Pipe auslesen
Stream := TMemoryStream.Create;
try
while true
do begin
succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead,
nil);
if not succeed
then break;
Stream.
Write(Buffer, NumberOfBytesRead);
end;
Stream.Position := 0;
Output.LoadFromStream(Stream);
finally
Stream.Free;
end;
//CloseHandle(PipeOutputRead);
//Fehler Read-Pipe auslesen
Stream := TMemoryStream.Create;
try
while true
do begin
succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead,
nil);
if not succeed
then break;
Stream.
Write(Buffer, NumberOfBytesRead);
end;
Stream.Position := 0;
Errors.LoadFromStream(Stream);
finally
Stream.Free;
end;
//CloseHandle(PipeErrorsRead);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
//CloseHandle(ProcessInfo.hProcess);
end
else begin
result:=false;
CloseHandle(PipeOutputRead);
CloseHandle(PipeOutputWrite);
CloseHandle(PipeErrorsRead);
CloseHandle(PipeErrorsWrite);
end;
end;
function SendCommandDone(
const Command:
String;
var Output, Errors: TStringList): Boolean;
var
Succeed: Boolean;
Buffer:
array [0..255]
of Char;
NumberOfBytesRead: DWORD;
Stream: TMemoryStream;
begin
//Ausgabe Read-Pipe auslesen
Stream := TMemoryStream.Create;
try
while true
do begin
succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead,
nil);
if not succeed
then break;
Stream.
Write(Buffer, NumberOfBytesRead);
end;
Stream.Position := 0;
ConsoleOutput.LoadFromStream(Stream);
finally
Stream.Free;
end;
//CloseHandle(PipeOutputRead);
//Fehler Read-Pipe auslesen
Stream := TMemoryStream.Create;
try
while true
do begin
succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead,
nil);
if not succeed
then break;
Stream.
Write(Buffer, NumberOfBytesRead);
end;
Stream.Position := 0;
ConsoleErrors.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
initialization
ConsoleOutput := TStringList.Create;
ConsoleErrors := TStringList.Create;
finalization
ConsoleErrors.Free;
ConsoleOutput.Free;
end.
program p_console_read_write;
uses
SysUtils,
Classes,
IPCConsole
in '
IPCConsole.pas';
var
Command:
String;
procedure GetOutputFrom(
var OutList: TStringList);
var cnt,i: Integer;
begin
if Assigned(OutList)
then
begin
i := 0;
cnt := OutList.Count;
while i<cnt
do
begin
writeln('
#### AUSGABE ####::: ',OutList[i]);
inc(i);
end;
Writeln; Writeln;
end;
end;
begin
if StartProcessDone('
MyProcess.exe')
then
repeat
Write('
Give a Command: '); Readln(Command);
if SendCommandDone(Command, ConsoleOutput, ConsoleErrors)
then GetOutputFrom(ConsoleOutput);
until Command = '
q';
end.