Creating a console from a windows subsystem program

Sometimes, printing messages and dumping variables can be very handy for debugging purposes, but most of today's programs do not need and in fact do not open a console window.

The following is a way a console can be opened from a windows subsystem application.

// Open the console window
AllocConsole();

// Assign the stdin/stdout/stderr streams to the newly created console
_tfreopen_s(&fpStdIn, _T("CONIN$"), _T("r"), stdin);
_tfreopen_s(&fpStdOut, _T("CONOUT$"), _T("w"), stdout);
_tfreopen_s(&fpStdErr, _T("CONOUT$"), _T("w"), stderr);

Once opened and initialized, the console can be used just like you do in a normal text-based application. Note that you need to explicitly release both the console and the streams you opened.

// Release the console
FreeConsole();

// Release the streams
fclose(stdin);
fclose(stdout);
fclose(stderr);

It's a good idea to add this code inside an #ifdef statement, in order to remove this code (as well as the printf functions) from the release version.

Source code: http://www.InsaneDevelopers.net/sources/CreatingAConsole.zip


Handling CTRL+C in your console application

First of all, to make sure we can receive this event, the ENABLE_PROCESSED_INPUT console mode must be enabled using SetConsoleMode; without this, the CTRL+C signal would be sent as a keystroke inside the input buffer.

SetConsoleMode(hdlStdInput, ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE);

We can now register our HandlerRoutine function using SetConsoleCtrlHandler

BOOL WINAPI blCtrlCHandler(DWORD dwCtrlType) { ... }
...
SetConsoleCtrlHandler(blCtrlCHandler, TRUE);

The handler function can be also handy for cleanup code, since it does not receives only the CTRL+C signal, but other events as well. Checking the dwCtrlType variable allows you to detect CTRL+BREAK and system events like logoff, shutdown, and the program's termination through the close button.

If you don't need the CTRL+C signal, and you want to mask it, it is possible to register a null function pointer that will prevent the default handler from handling the signal.

SetConsoleCtrlHandler(NULL, TRUE);

Source code: http://www.insanedevelopers.net/sources/CTRL-CHandling.zip


Application monitoring

I've been asked if there's a way to programmatically determine if an application crashed in order to restart it; the answer is, of course, yes. To accomplish this task, you have to create the process with a launcher that acts like a debugger; your application only needs to check the exit code of the process whenever it receives an EXIT_PROCESS_DEBUG_EVENT event (you can also check for unrecoverable exceptions).

Here's the pseudo-code:

DEBUG_EVENT deDebugEvent;
...
WaitForDebugEvent(..);
...
// Check the event type
switch(deDebugEvent.dwDebugEventCode)
{
	...
	case EXIT_PROCESS_DEBUG_EVENT:
	{
		// We assume that if the program exited with a nonzero code, it crashed
		if (deDebugEvent.u.ExitProcess.dwExitCode)
		{
			// The program crashed
			DebugActiveProcessStop(...);
			CreateProcess(...);
			...
		}

		// Clean exit
		ExitProcess(0);
	}
	...
}

Source code: http://www.insanedevelopers.net/sources/ApplicationMonitor.zip


Modeling the female torso [Second Ed] HD Playlist


Modeling the female torso [Second Ed] Playlist


Modeling the female torso [First Ed] Playlist