How to send to console's StandardInput from GUI app?

D

Dv

When our application is launched from Dos console, we need to print
some message to the console. I attached our application to the console
using AttachConsole(ATTACH_PARENT_PROCESS), and then call
Console.WriteLine(...) to print some messages there. Everything works
fine, except that after the message has been printed, it'll require
user to press any key exit from there.

So, I'd like to insert a key stroke to the StandardInput
programmtically. I know some kernel32.dll APIs can do that. But I
wonder if there're any equivalent C# functions?
 
M

Morten Wennevik [C# MVP]

Dv said:
When our application is launched from Dos console, we need to print
some message to the console. I attached our application to the console
using AttachConsole(ATTACH_PARENT_PROCESS), and then call
Console.WriteLine(...) to print some messages there. Everything works
fine, except that after the message has been printed, it'll require
user to press any key exit from there.

So, I'd like to insert a key stroke to the StandardInput
programmtically. I know some kernel32.dll APIs can do that. But I
wonder if there're any equivalent C# functions?

Hi DV,

I'm not familiar with AttachConsole, and as it is a kernel32 method, I'm not
surprised if you have to use kernel32 to further tweak the Console. You can
change StandardInput, StandardOutput and StandardError of a Console using the
SetIn, SetOut and SetError methods. Maybe setting StandardInput after
AttachConsole will give you what you need.

I'm not sure why you would need to write anything to the console if you have
a windows application, although there may be valid reasons I'm not aware of.
However, why do you need the input from console if you only want to override
it anyway?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top