Hallo, vielleicht hilft dir dieser code hier weiter um eine consolen anwendung auszuführen die keinerlei input bedarf.
Du erhälst den Output erst nachdem die Consolen andwendung fertig ist (terminiert)
Den pChar output kannst du dann nach belieben auswerten oder per Edit/Memo anzeigen lassen.
viel spass damit.
ps: der schalter ShowConsole ist mehr oder weniger ein fake,
es wird zwar die console geöffnet aber der komplette inhalt landet nach wie vor im "Output:pChar"
Ist sowas wie 'ne kontrolle, wenn console geschlossen = anwendung fertig
Delphi-Quellcode:
// this function will execute a console application
// give all console output back to "Output:PChar" parameter
// give back as Result the ExitCode of "Executable"
// ShowConsole should only be activated if you want to see a blank Console Window aslong Process is running
// WARNING: DO NOT EXECUTE CONSOLE APPLICATIONS THAT NEED KEYBOARD/MOUSE INPUT!!!
// if result is 4294967295/$FFFFFFFF than its a general error
Function ExecuteConsole(const Executable: String; Commands: String; const ShowConsole: Boolean; var Output: PChar): Cardinal;
const
ReadBuffer = 4000;
var
Security : TSecurityAttributes;
ReadPipe,WritePipe : THandle;
StartupInfo : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pchar;
BytesRead : DWord;
AppRunning : DWord;
begin
Result := Cardinal($FFFFFFFF);
if ((Length(Commands) > 1)and(Commands[1]<>' ')) then Commands := ' ' + Commands;
with Security do begin
nLength := SizeOf(TSecurityAttributes);
lpSecurityDescriptor := nil;
bInheritHandle := True;
end;
if CreatePipe (ReadPipe, WritePipe, @Security, 0) then
begin
Buffer := AllocMem(ReadBuffer + 1);
FillChar(StartupInfo,SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.hStdOutput := WritePipe;
StartupInfo.hStdInput := ReadPipe;
StartupInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
if ShowConsole then StartupInfo.wShowWindow := SW_SHOWNORMAL else StartupInfo.wShowWindow := SW_HIDE;
if CreateProcess(PChar(Executable), PChar(Commands), @Security, @Security, True, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(Executable)), StartupInfo, ProcessInfo) then
begin
repeat
ProcessMessages;
Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
until (Apprunning <> WAIT_TIMEOUT);
// if DoWait then while WaitForSingleObject(ProcessInfo.hProcess, 500) <> WAIT_OBJECT_0 do ProcessMessages;
repeat
BytesRead := 0;
ReadFile(ReadPipe,Buffer[0], ReadBuffer, BytesRead,nil);
Buffer[BytesRead]:= #0;
OemToChar(Buffer, Output);
until (BytesRead < ReadBuffer);
end;
FreeMem(Buffer);
if not GetExitCodeProcess(ProcessInfo.hProcess, Result) then Result := Cardinal($FFFFFFFF);
// GetExitCodeProcess(ProcessInfo.hProcess, Cardinal(Result));
CloseHandle(ReadPipe);
CloseHandle(WritePipe);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
end;