Delphi XE8:问题 运行 一个外部控制台应用程序,正在等待其结果并捕获其结果
Delphi XE8: problems running an external console application, waiting for its results and capturing its results
在 Windows 下的 Delphi XE8 中,我试图调用外部控制台应用程序并捕获其输出。我使用以下代码,如 Capture the output from a DOS (command/console) Window and also Getting output from a shell/dos app into a Delphi app:
中所述
procedure TForm1.Button1Click(Sender: TObject) ;
procedure RunDosInMemo(DosApp:String;AMemo:TMemo) ;
const
ReadBuffer = 2400;
var
Security : TSecurityAttributes;
ReadPipe,WritePipe : THandle;
start : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pchar;
BytesRead : DWord;
Apprunning : DWord;
S: String;
begin
With Security do begin
nlength := SizeOf(TSecurityAttributes) ;
binherithandle := true;
lpsecuritydescriptor := nil;
end;
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;
S:=UniqueString(DosApp);
if CreateProcess(nil,
PChar(S),
@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;
begin {button 1 code}
RunDosInMemo('cmd.exe /c dir',Memo1) ; //<-- this works
RunDosInMemo('"c:\consoleapp.exe" "/parameter"',Memo1) //<-- this hangs in the repeat until (Apprunning <> WAIT_TIMEOUT) ;
end;
它适用于 DOS 命令,但不适用于控制台应用程序。控制台应用程序启动并正确执行,但它挂在 repeat until (Apprunning <> WAIT_TIMEOUT)
循环中。我可以尝试什么来解决这个问题?
非常感谢!
您的程序 运行 正在等待输入(例如来自键盘的输入)或产生的输出超出管道缓冲区的容量。在任何一种情况下,该程序都会挂起等待进一步的 I/O 操作,但您的 parent 程序正在等待 child 在处理任何输出之前终止,并且从不提供任何输入。
您需要在程序仍在运行时处理输出管道 运行。否则,您可能会冒缓冲区被填满的风险,并且 child 将阻塞,直到有更多 space 可用为止。同样,如果您不打算向其他进程提供任何输入,您可能不应该给它一个有效的输入句柄。这样,如果它尝试读取输入,它将失败,而不是阻塞。
此外,您为该程序提供的输入句柄附加到输出句柄。如果程序尝试读取,它将读取自己的输出,如 I/O ouroboros。要处理输入 和 输出,您需要 两个 管道。
(请注意,这个死锁正是问题 I called out in the comments of the answer you used。同一个答案中的第二个代码块解决了这个问题,以及 ouroboros 问题。)
总结一下:@David Heffernan 在 Execute DOS program and get output dynamically 中的代码有效。问题是控制台应用程序发出 UTF-16。
在 Windows 下的 Delphi XE8 中,我试图调用外部控制台应用程序并捕获其输出。我使用以下代码,如 Capture the output from a DOS (command/console) Window and also Getting output from a shell/dos app into a Delphi app:
中所述procedure TForm1.Button1Click(Sender: TObject) ;
procedure RunDosInMemo(DosApp:String;AMemo:TMemo) ;
const
ReadBuffer = 2400;
var
Security : TSecurityAttributes;
ReadPipe,WritePipe : THandle;
start : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pchar;
BytesRead : DWord;
Apprunning : DWord;
S: String;
begin
With Security do begin
nlength := SizeOf(TSecurityAttributes) ;
binherithandle := true;
lpsecuritydescriptor := nil;
end;
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;
S:=UniqueString(DosApp);
if CreateProcess(nil,
PChar(S),
@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;
begin {button 1 code}
RunDosInMemo('cmd.exe /c dir',Memo1) ; //<-- this works
RunDosInMemo('"c:\consoleapp.exe" "/parameter"',Memo1) //<-- this hangs in the repeat until (Apprunning <> WAIT_TIMEOUT) ;
end;
它适用于 DOS 命令,但不适用于控制台应用程序。控制台应用程序启动并正确执行,但它挂在 repeat until (Apprunning <> WAIT_TIMEOUT)
循环中。我可以尝试什么来解决这个问题?
非常感谢!
您的程序 运行 正在等待输入(例如来自键盘的输入)或产生的输出超出管道缓冲区的容量。在任何一种情况下,该程序都会挂起等待进一步的 I/O 操作,但您的 parent 程序正在等待 child 在处理任何输出之前终止,并且从不提供任何输入。
您需要在程序仍在运行时处理输出管道 运行。否则,您可能会冒缓冲区被填满的风险,并且 child 将阻塞,直到有更多 space 可用为止。同样,如果您不打算向其他进程提供任何输入,您可能不应该给它一个有效的输入句柄。这样,如果它尝试读取输入,它将失败,而不是阻塞。
此外,您为该程序提供的输入句柄附加到输出句柄。如果程序尝试读取,它将读取自己的输出,如 I/O ouroboros。要处理输入 和 输出,您需要 两个 管道。
(请注意,这个死锁正是问题 I called out in the comments of the answer you used。同一个答案中的第二个代码块解决了这个问题,以及 ouroboros 问题。)
总结一下:@David Heffernan 在 Execute DOS program and get output dynamically 中的代码有效。问题是控制台应用程序发出 UTF-16。