2

What exactly happens when a .NET console application starts?

In the process explorer, when starting the exe I am wondering why I cannot see a "cmd.exe" process as a parent process for the console application. What exactly is displayed then?

Is there a way to replace the "default" console window by another one? I guess this would mean modifying the "console subsystem".

Creating a GUI application instead of a console application is not an option as I do not have the source of all possible tools.

Observation:

  • With Mono and Linux, I have no issue at all regarding this and my test app!
  • The font used has an influence, I cannot find a font that fits for everything (even with asia pack installed)
  • Tweaking (Changing font, sizes, ...) in registry at HKEY_CURRENT_USER\Console is having an impact and can be defined per executable.
jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • Is anything in this answer useful? http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using/1259468#1259468 – Graham Clark Sep 24 '10 at 09:15

2 Answers2

3

In the process explorer, when starting the exe I am wondering why I cannot see a "cmd.exe" process as a parent process for the console application. What exactly is displayed then?

You don't need cmd.exe to have a console window, any executable with the correct header flag will cause Windows to create a console for it, or connect to the console window of its parent process if its parent has one.

Is there a way to replace the "default" console window by another one?

Only by:

  • Changing the compiler flags for your application to be a GUI application and then using the Win32 API AllocConsole to create a console. (Included for completeness, won't apply here if you cannot rebuild the executable).1
  • Creating an intermediate program that is not a console application (so not associated with its parent console) which then launches your program.
  • Creating an intermediate program that is a console application (so not associated with its parent console) which then launches your program with the CREATE_NEW_CONSOLE flag passed to CreateProcess.2

1 Editbin.exe can change the flag (editbin /subsystem:WINDOWS), but the application would then need to call AllocConsole.

2 It is not clear if the CreateNoWindow property of ProcessStartInfo serves the same function for Process.Start in .NET. If it does this intermediary could be written in .NET, but a native solution would be considerably lighter weight—in such a short program having to load .NET will significantly slow things down.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Thanks now I understand how the magic is done. I always thought erroneoously it was something built into the application. This makes a system-wide cmd.exe (theoretically at least) possible. – jdehaan Sep 27 '10 at 06:41
2

Try to set the Console.OutputEncoding property.

Console.OutputEncoding = Encoding.UTF8;

This will effectively call the Win32 native SetConsoleOutputCP function setting the console code page to the CodePage property of the specified encoding.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • That is definitively one of the things to do, beside installing support for asian languages. But the fonts, neither raster nor "Lucida Console" are able to display the kanji correctly. With some tricks the font "Consolas" worked. So it turns out cmd.exe unterstands UTF8 but cannot display all chars properly. Still I would like to know how the command line gets integrated into a console application, how tied it is to it and if it's possible to provide a replacement for it. – jdehaan Sep 24 '10 at 09:38
  • FWIW the font Japanese Windows uses in the terminal is ‘MS Gothic’. – bobince Sep 24 '10 at 17:29