Mam aplikację którą uruchamiam w pewnym miejscu w programie aplikacja uruchamiana w konsoli po skończeniu pracy wywala "naciśnij dowolny klawisz aby kontynuować".I chcę tak zrobić zeby delphi pobierało ostatni wiersz z konsoli ,jest to do zrobienia? [soczek]
0
0
a szukales na forum?
unitedcmd.
0
Oczywiście, że jest do zrobienia. Można użyć modułu, o którym pisał poprzednik albo wygoogluj TDosCommand.
0
Moze to ci sie przyda:
procedure CaptureConsoleOutput(DosApp : string; var wynik : string);
const
ReadBuffer = 1048576; // 1 MB Buffer
var
Security : TSecurityAttributes;
ReadPipe,WritePipe : THandle;
start : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pansichar;
TotalBytesRead,
BytesRead : DWORD;
Apprunning,n,
BytesLeftThisMessage,
TotalBytesAvail : integer;
begin
with Security do
begin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
if CreatePipe (ReadPipe, WritePipe, @Security, 0) then
begin
// Redirect In- and Output through STARTUPINFO structure
SetCurrentDir(ExtractFileDir( ParamStr(0)) ) ;
Buffer := AllocMem(ReadBuffer + 1);
FillChar(Start,Sizeof(Start),#0); //!!wazne
start.cb := SizeOf(start);
start.hStdOutput := WritePipe;
start.hStdInput := ReadPipe;
start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
// Create a Console Child Process with redirected input and output
//UniqueString(DosApp);
if CreateProcess( nil ,
PChar(DosApp),
@Security,@Security,
true ,
CREATE_NO_WINDOW + NORMAL_PRIORITY_CLASS,
nil,
nil,
//pchar(ExtractFileDir(DosApp)),
start ,ProcessInfo) then
begin
n:=0;
TotalBytesRead:=0;
repeat
// Increase counter to prevent an endless loop if the process is dead
Inc(n,1);
// wait for end of child process
Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
Application.ProcessMessages;
// it is important to read from time to time the output information
// so that the pipe is not blocked by an overflow. New information
// can be written from the console app to the pipe only if there is
// enough buffer space.
if not PeekNamedPipe(ReadPipe ,@Buffer[TotalBytesRead],
ReadBuffer ,@BytesRead,
@TotalBytesAvail,@BytesLeftThisMessage) then break
else if BytesRead > 0 then
ReadFile(ReadPipe,Buffer[TotalBytesRead],BytesRead,BytesRead,nil);
TotalBytesRead:=TotalBytesRead+BytesRead;
until (Apprunning <> WAIT_TIMEOUT) or (n > 250);
//Buffer[TotalBytesRead]:= #0;
//OemToChar(Buffer,Buffer);
//SetLength(res,TotalBytesRead);
//OemToChar(Buffer,PChar(res));
wynik := string(Buffer);
if not ((Length(wynik) > 4) and (Length(wynik)<10)) then
wynik := 'ERROR';
end;
FreeMem(Buffer);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
CloseHandle(WritePipe);
end;
end;
Przyklad uzycia:
CaptureConsoleOutput('ipconfig /all' ,w);
// wrzuć to, jeśli możesz, do gotowców albo do faq, albo do jednego i drugiego - Ł