Method 1: Pause if Process is Running in Separate ConsoleThe 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.
hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi))
printf("GetConsoleScreenBufferInfo failed: %d\n", GetLastError());
// if cursor position is (0,0) then use pause
bUsePause = ((!csbi.dwCursorPosition.X) &&
printf("Interesting information to read.\n");
printf("More interesting information to read.\n");
// only pause if running in separate console window.
printf("\n\tPress any key to exit...\n");
ch = getch();
Method 2: Start the console with cmd.exe /KThis 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:
Article ID: 99115 - Last Review: Nov 21, 2006 - Revision: 1