There are files that are Win32 console applications written in C. When you try to start a file by double-clicking on it, the application runs and the console window is immediately minimized. How to avoid it?

getchar ();

added to the end of the program allows you to open them with the right click and the Open item from the drop-down list, double-click they do not open, but open triple. How to run these files with the usual double click?

  • 3
    Sorry, I wouldn’t believe that adding a call to getchar() makes the exe-file not launch by double-clicking ... You do not finish speaking something. - Harry
  • What side is there at all C ? I assure you that programs written in Pascal, assembler or Perle behave the same way. - PinkTux
  • 3
    In general, console programs must be launched from the console :) - PinkTux
  • I experimented, without a getchar, the window is always minimized; neither the triple click nor the right button with the item opens the context menu, if getchar (); before return 0; This helps triple-click and the right button with the item open the context menu. If getchar (); after return 0; then double click works, but you don’t seem to do that? The compiler swears - Victor
  • one
    I have a great suspicion that you misunderstand something, and accordingly mislead us ... getchar() just stops the program execution while waiting for input with pressing the Enter key, and has nothing to do with the number of clicks to start ... - Harry

1 answer 1

When you try to start a file by double-clicking on it, the application runs and the console window immediately collapses.

When the process for which the console window was created, completes its work, the operating system immediately destroys the window . getch() you simply “freeze” the program while waiting for the entered character, thereby delaying its completion.


How does this happen? When starting the program, the PE EXE loader looks at the value of its “subsystem” field and, seeing the WINDOWS_CUI value WINDOWS_CUI , requests the console subsystem ( conhost.exe ) to create a window. Then the streams of this window are bound to standard I / O streams. Further, conhost monitors the status of these threads.

If this process starts up some other processes and passes the already existing I / O handles to them, the console subsystem will not worry about their completion. For example, if we run cmd , and from there your program, then the completion of this program will not result in closing the window . But if you complete the cmd itself (using the exit , for example), the console will instantly close.

PS: I do not know the motives for such behavior, exactly as the reasons for the impossibility of forcing the phrase “The program has completed its work. Press any key to close the window by the console subsystem. Just take it as given.

  • And how does the behavior described by the TS "explain by double-clicking, they do not open, but open triple"? - Harry
  • @Harry, did not understand about the triple click. In Windows, the standard only single and double exist, is not it? Perhaps the author of the question is some kind of stray , and he keeps back something. - ߊߚߤߘ
  • Here I am about the same! - Harry
  • Triple click, after two clicks the window disappears, but another quick click after the second one fixes the window - Victor
  • Arhad, you wrote that "For example, if we run cmd, and from it - your program, then the completion of this program will not result in closing the window" It is not so, if I run my program from the command line, that window of the executable application appears seconds and then evaporates - Victor