gpt4 book ai didi

Delphi 6 - 在运行时读取控制台应用程序的输出

转载 作者:行者123 更新时间:2023-12-03 14:43:07 25 4
gpt4 key购买 nike

如何读取正在运行的控制台应用程序输出。我启动控制台应用程序,并希望读取控制台应用程序打印的输出。

最佳答案

这个怎么样solution .

<小时/>

编辑:链接指向此解决方案(为了易读性而进行了轻微重构,并删除了 with 的使用):

// The example runs 'chkdsk.exe c:\' and displays the output to Memo1.
// Put a TMemo (Memo1) and a TButton (Button1) on your form. Put this
// code in the OnCLick event procedure for Button1:

procedure TForm1.RunDosInMemo(DosApp:String;AMemo:TMemo) ;
const
ReadBuffer = 2400;
var
Security : TSecurityAttributes;
ReadPipe,
WritePipe : THandle;
start : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pchar;
BytesRead : DWord;
Apprunning : DWord;

begin
Security.nlength := SizeOf(TSecurityAttributes) ;
Security.binherithandle := true;
Security.lpsecuritydescriptor := nil;
if Createpipe (ReadPipe, WritePipe, @Security, 0) then
begin
Buffer := AllocMem(ReadBuffer + 1) ;
FillChar(Start,Sizeof(Start),#0) ;
start.cb := SizeOf(start) ;
start.hStdOutput := WritePipe;
start.hStdInput := ReadPipe;
start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
if CreateProcess(nil,
PChar(DosApp),
@Security,
@Security,
true,
NORMAL_PRIORITY_CLASS,
nil,
nil,
start,
ProcessInfo) then
begin
repeat
Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
Application.ProcessMessages;
until (Apprunning <> WAIT_TIMEOUT) ;
repeat
BytesRead := 0;
ReadFile(ReadPipe,Buffer[0],
ReadBuffer,BytesRead,nil) ;
Buffer[BytesRead]:= #0;
OemToAnsi(Buffer,Buffer) ;
AMemo.Text := AMemo.text + String(Buffer) ;
until (BytesRead < ReadBuffer) ;
end;
FreeMem(Buffer) ;
CloseHandle(ProcessInfo.hProcess) ;
CloseHandle(ProcessInfo.hThread) ;
CloseHandle(ReadPipe) ;
CloseHandle(WritePipe) ;
end;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
begin
RunDosInMemo('chkdsk.exe c:\', Memo1) ;
end;

关于Delphi 6 - 在运行时读取控制台应用程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1212176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com