Windows Application - attach to the existing console

  • Thread starter Thread starter Oleg.Ogurok
  • Start date Start date
O

Oleg.Ogurok

Hi there,

I wrote an app that has GUI but also accepts command line parameters
for automated execution.
When the app is started from command line with parameters, I'd like the
application to behave like a Console Application, e.g.
* the command prompt should not return right away but only once the
application quits
* Console.Write() should write into the console

Is this possible? e.g. via .NET SDK or PInvoke/Win32?

Thanks,
-Oleg.
 
Hello Oleg,

OO> I wrote an app that has GUI but also accepts command line parameters
OO> for automated execution.
OO> When the app is started from command line with parameters, I'd like
OO> the
OO> application to behave like a Console Application, e.g.

What should happen to GUI?

OO> * the command prompt should not return right away but only once
OO> the
OO> application quits
OO> * Console.Write() should write into the console
OO> Is this possible? e.g. via .NET SDK or PInvoke/Win32?

I think that u need to look at AttachConsole API

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
have a condition in ur Main(), based on which u show the GUI.
something like this
Main()
{
if(showGui) //if command line args contains flag
{
Application.Run(mainForm())
}
else
{
//run as service mode without any UI
}
}
 
Michael said:
I think that u need to look at AttachConsole API

....and/or AllocConsole and FreeConsole. You may have to map the
Console.Out stream into the console window you attached with
AttachConsole. I believe I have gotten this work before, but I cannot
remember exactly what I did. I think the Console.Out stream will map
into a new console created by AllocConsole automatically, but don't
take my word for it. The problem with AllocConsole is that it will
create a brand new console window even if the application is launched
from an existing console window. That can be annoying depending on how
you want it to work.
 
Back
Top