6

I am writing a windowed .NET app in C#, which runs a third party console application via the Process class, as hidden as possible (CreateNoWindow, RedirectStandardOutput, etc.).

I've redirected it's StandardInput, so I can write whatever string I want, but not function keys or other type of special keys, as they don't have a character representation. As for me, I have to send keys F1 to F4 to the console app. The solutions I've found are for windowed app (PostMessage, SendMessage).

How can I do this for my console application?

Is there anything to do with the Handle of the Process?

ifroz
  • 103
  • 3
  • 6

4 Answers4

1

You can't. Function keys cannot work with stdin, an app has to call ReadConsoleInput() to be able to detect them. That no longer works when you start the process without a console window.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • There is no way to push some data to the buffer it reads, is there? – ifroz Jun 25 '10 at 11:20
  • No console window == no buffer. – Hans Passant Jun 25 '10 at 11:59
  • I'm not buying this answer. My application gets terminated when I press CTRL-BREAK, yet it has no such call to ReadConsoleInput, so that can't possibly have anything to do with it. Since the command console has the ability to start a process, and send a break signal to it, I can't see any reason why some other application that starts a process couldn't send the same kind of signal. – Triynko Apr 19 '13 at 19:11
  • You don't have to buy it, this answer is free. Ctrl+Break is handled specially, review SetConsoleCtrlHandler() to see how it is done. – Hans Passant Apr 19 '13 at 20:22
  • I'm quite familiar with SetConsoleCtrlHandler as well as the default method that handles the event kernel32!CtrlRoutine which one can call directly by identifying its address and starting a remote thread with it. I'm also familiar with GenerateConsoleCtrlEvent, which is incredibly broken and fails to work whether a console app process is started in a new process group or not and whether its started with a console window or not (whether using create_new_console, create_no_window, or detached_process process creation flags). Even if you have to call AllocConsole and HideWindow, there is a way. – Triynko Apr 22 '13 at 19:34
0

Sendkeys.SendWait may solve your problem.

nothrow
  • 15,882
  • 9
  • 57
  • 104
  • SendKeys can only send keystrokes to the active application, which is unfortunately not the case here. – ifroz Jun 25 '10 at 10:36
0

Does SendKeys work ("{F1}")? (the console will have to be active, though).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    The console itself does not even exist as a window (Process.CreateNoWindow), so SendKeys is not applicable in this case. – ifroz Jun 25 '10 at 10:35
0

This isn't directly an answer to your question, but have you considered using MSMQ?

If your windowed application can receive those key-presses, it could pass that fact on to your console application using MSMQ. You may not have to actually pass the key-press, just the the fact that they were pressed.

I have found this techrepublic article helpful in getting the basics of MSMQ working.

Community
  • 1
  • 1
Jonathan
  • 25,873
  • 13
  • 66
  • 85