【发布时间】:2017-05-25 00:41:12
【问题描述】:
如何让 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)。
这是我的 Write Pipe 和 Send 过程:
写管道
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 按下,只是无法弄清楚如何实现这一点。我将非常感谢任何指导。
【问题讨论】: