procedure MyNew(const Filename, Parameter: string);
var
CommandLine : string;
si : STARTUPINFO;
pi : PROCESS_INFORMATION;
sa : SECURITY_ATTRIBUTES;
newstdout : THandle;
write_stdin : THandle;
read_stdout : THandle;
Output : string;
buffer : array[0..1023] of char;
ExitCode : cardinal;
procedure CorrectCRLF(var s: string); // macht aus CR: CR + LF
begin
s := StringReplace(s, char($0D), char($0D) + char($0A), [rfReplaceAll]);
s := StringReplace(s, char($0D) + char($0A) + char($0A), char($0D) + char($0A), [rfReplaceAll]);
end;
procedure ZeroBuffer(var buffer2clear: array of char);
begin
FillChar(buffer2clear, SizeOf(buffer2clear), 0);
end;
procedure GetData;
var
bytes_read : cardinal;
bytes_available : cardinal;
begin
PeekNamedPipe(read_stdout, @buffer, sizeof(buffer), @bytes_read, @bytes_available, NIL);
if bytes_read > 0 then
begin
ZeroBuffer(buffer);
if (bytes_available > sizeof(buffer)) then
begin
while (bytes_read >= sizeof(buffer)) do
begin
ReadFile(read_stdout, buffer, sizeof(buffer), bytes_read, NIL);
Output := Output + buffer;
CorrectCRLF(Output);
ZeroBuffer(buffer);
end;
end
else
begin
ReadFile(read_stdout, buffer, sizeof(buffer), bytes_read, NIL);
Output := Output + buffer;
CorrectCRLF(Output);
end;
end;
end;
begin
CommandLine := '"' + Filename + '" ' + Parameter;
GetStartupInfo(si);
sa.nLength := SizeOf(sa);
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := true;
CreatePipe(read_stdout, newstdout, @sa, 0);
CreatePipe(write_stdin, newstdin, @sa, 0);
si.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
si.wShowWindow := SW_SHOW;
si.hStdOutput := newstdout;
si.hStdError := newstdout;
si.hStdInput := write_stdin;
if not (CreateProcess(NIL, PChar(CommandLine), NIL, NIL, TRUE, CREATE_NEW_CONSOLE, NIL, NIL, si, pi)) then
begin
CloseHandle(newstdout);
CloseHandle(read_stdout);
CloseHandle(newstdin);
CloseHandle(write_stdin);
SM('Fehler bei CreateProcess: ' + inttostr(GetLastError));
end;
GetExitCodeProcess(pi.hProcess, ExitCode);
while ExitCode = STILL_ACTIVE do
begin
Application.ProcessMessages;
GetData;
GetExitCodeProcess(pi.hProcess, ExitCode);
MainForm.Memo1.Text := Output;
end;
GetData;
MainForm.Memo1.Text := Output;
CloseHandle(newstdout);
CloseHandle(read_stdout);
CloseHandle(newstdin);
CloseHandle(write_stdin);
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
buffer : char;
bytes_written : cardinal;
begin
buffer := 'n';
WriteFile(newstdin, buffer, sizeof(buffer), bytes_written, nil);
end;