1

I am attempting to make my GUI app have a CLI frontend (not the opposite).

I have been able to process arguments without an issue (thanks to System.Environment.GetCommandLineArgs()), but I am curious what the best way to gain access to write output to the console.

I have found reference to AllocConsole(), but it is unclear if this is the only method, as I'd much prefer something revealed in managed code.

ckittel
  • 6,478
  • 3
  • 41
  • 71
brandeded
  • 2,080
  • 6
  • 25
  • 52

1 Answers1

2

There's also AttachConsole but that's always a mistake since it allows output to get intermingled.

AllocConsole() is it. Just creating a form that acts like a console would be the more Winformy way. With the considerable advantage that the user closing it doesn't cause your program to abort.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I actually don't wish for a console interface to _co-exist_ with the forms interface; therefore your method isn't important for my current usage, but may be for other usage requirements. If I use AllocConsole(), [as expected](http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx) a new console was spawned. It appears that [AttachConsole()](http://www.pinvoke.net/default.aspx/kernel32/AttachConsole,.html) will allow me to attach to the console that is the cmd.exe host process! Thanks! – brandeded Aug 19 '11 at 15:47
  • for future reference: I was stuck with another problem and this hacky solution worked: http://stackoverflow.com/questions/1305257/using-attachconsole-user-must-hit-enter-to-get-regular-command-line/2463039#2463039 ewww. – brandeded Aug 19 '11 at 16:47