0

How can I get Delphi to pass F4 to the input pipe to a CMD process? I've been using code which is very similar to the answer here: Pipes in Delphi for Command Prompt

For simpliity I have only 4 objects on the form: 1. CommandText (a TMemo which displays a representation of the CMD window). 2. CommandRun (a TEdit where the CMD line to be run is entered). 3. SendBtn (a TButton used to send CommandRun.Text to CMD). 3. ExitBtn (a TButton used to exit the program).

Here are my Write Pipe and Send procedures:

Write Pipe

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;

Send

procedure TForm1.SendBtnClick(Sender: TObject);
begin
   WritePipeOut(OutputPipeWrite, CommandRun.Text);
   CommandRun.Text := '';
   CommandRun.SetFocus;
end;

Commands like DIR are passing through properly and the results are successfully being 'echoed' to CommandText. My problem is that I am launching a program via the pipe. In a normal CMD window the program can only be stopped by pressing the F4 key. I need to replicate this F4 press via the pipe and just cannot work out how to achieve this. I would be very grateful for any guidance.

Community
  • 1
  • 1
Paul
  • 13
  • 4

1 Answers1

0

F4 isn't something that would be transmitted through the standard input. This console application is almost certainly reading the keyboard input directly.

You will need to look for another way to solve this. I suggest the following:

  1. Make sure that the cmd window has input focus and fake the input of an F4 key. I would imagine that moving the focus to the cmd window would be something that you would be reluctant to do.
  2. Use another means to stop this process. Whether or not the process supports anything other than an F4 keypress to the console window cannot be determined from here since we know nothing about the process in question.
  3. Forcefully terminate the process, with a call to TerminateProcess.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks @David. You've confirmed my fears. Looks like I'll have have to use a sledgehammer and use taskkill.exe /F /IM MyApp.exe – Paul Jan 11 '17 at 11:53
  • Why are you doing so much with external processes? Why can't you call API functions. – David Heffernan Jan 11 '17 at 12:00
  • Indeed! I got side-tracked and wanted to investigate pipes and threads as I hadn't used them before. – Paul Jan 11 '17 at 13:38
  • "WriteConsoleInput" is what can write function keys to a console input buffer, can send a key event with a virtual key code. However, a standard handle, once redirected, can only be used by "Read/WriteFile". I can't think of any way to get an input buffer handle without cooperation of the process that owns the console. OP has a lot to read on consoles and pipes to find out if it is actually possible at all. – Sertac Akyuz Jan 11 '17 at 23:15