如何让Delphi将F4传递到CMD进程的输入管道?我一直在使用与此处答案非常相似的代码:Pipes in Delphi for Command Prompt
为简单起见,我在表单上只有4个对象: 1. CommandText(显示CMD窗口表示的TMemo)。 2. CommandRun(输入要运行的CMD行的TEdit)。 3. SendBtn(用于将CommandRun.Text发送到CMD的TButton)。 3. ExitBtn(用于退出程序的TButton)。
以下是我的写管道和发送程序:
写管道
procedure WritePipeOut(OutputPipe: THandle; InString: string);
// writes Instring to the pipe handle described by OutputPipe
var
byteswritten: DWord;
AnsiBuf: AnsiString;
begin
// most console programs require CR/LF after their input.
if InString = 'f4' then AnsiBuf := #113#13#10
else AnsiBuf := AnsiString(InString) + #13#10;
WriteFile(InputPipeWrite, AnsiBuf[1], Length(AnsiBuf), byteswritten, nil);
end;
发送
procedure TForm1.SendBtnClick(Sender: TObject);
begin
WritePipeOut(OutputPipeWrite, CommandRun.Text);
CommandRun.Text := '';
CommandRun.SetFocus;
end;
像DIR这样的命令正常传递,结果成功地被回应'到CommandText。 我的问题是我通过管道启动程序。在正常的CMD窗口中,只能通过按F4键来停止程序。 我需要通过管道复制这台F4印刷机,但无法解决如何实现这一目标。我会非常感谢任何指导。
答案 0 :(得分:0)
F4 不是通过标准输入传输的东西。这个控制台应用程序几乎可以肯定直接读取键盘输入。
您需要寻找另一种解决方法。我建议如下:
cmd
窗口具有输入焦点并伪造 F4 键的输入。我认为将焦点移到cmd
窗口会是你不愿意做的事情。TerminateProcess
强行终止此过程。