INFO: Preventing the Console Window from Disappearing

Last reviewed: April 10, 1997
Article ID: Q99115
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.10, 3.50, 3.51, 4.0

SUMMARY

When a console application is started either from the File Manager, the Program Manager, Windows NT Explorer, or by typing "start <progname>" from the command prompt, it executes in its own console. This console disappears as soon as the application terminates, and therefore the user can't read anything written to the screen between the last pause and program exit. There are two approaches to keep the console window from disappearing.

MORE INFORMATION

Method 1: Pause if Process is Running in Separate Console

The first method is for implementing a console application to not terminate immediately when it is running in a separate console window. It is not likely that you would want an application to always pause after displaying information to the console window when you started from the prompt. However, there is no API (application programming interface) that directly determines whether or not the application shares a console with CMD.EXE. This method looks at the current location of the console cursor, and if it is (0,0), then the program assumes it is running in a separate console window.

Sample Code

   #include <windows.h>
   #include <stdio.h>
   #include <conio.h>

   CONSOLE_SCREEN_BUFFER_INFO csbi;
   HANDLE hStdOutput;
   BOOL bUsePause;

   void main(void)
   {
      hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
      if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi))
      {
         printf("GetConsoleScreenBufferInfo failed: %d\n", GetLastError());
         return;
      }

      // if cursor position is (0,0) then use pause
      bUsePause = ((!csbi.dwCursorPosition.X) &&
                   (!csbi.dwCursorPosition.Y));

      printf("Interesting information to read.\n");
      printf("More interesting information to read.\n");

      // only pause if running in separate console window.
      if (bUsePause)
      {
         int ch;
         printf("\n\tPress any key to exit...\n");
         ch = getch();
      }
   }

NOTE: This method will not work if the user combines a clear screen (CLS) and execution of the application into one step (for example, [C:\] CLS & <progname>), because the cursor position will be (0, 0), but the application is using the console, which belongs to CMD.EXE.

Method 2: Start the console with cmd.exe /K

This method is for starting a console application in a separate window and forcing the window to remain after the application has terminated. An application can use the following command line with WinExec(), CreateProcess(), or in a batch file:

   cmd /K consoleapp.exe

After consoleapp.exe has terminated, the /K switch makes the console window remain on the screen. The application user can then type the "exit" command to close the console window.


Keywords : BseCon kbprg
Version : 3.1 3.5 3.51 4.0
Platform : NT WINDOWS
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.