Attatching console to Win32 App?

I have a "Win32 App" in VC++ 2008 and I use the following code to attach a console for debugging purposes (while in or out of VC++):

1
2
3
4
5
6
7
8
#ifdef _DEBUG
	if(AllocConsole())
	{
		SetConsoleTitleA("Debug Console");
		freopen("CONOUT$", "wb", stdout);
		freopen("CONOUT$", "wb", stderr);
	}
#endif 


When I do "Debug" it attaches the console. I use something like this throughout the app for debugging reasons:

std::cout << "Debug message" << std::endl;

I don't wrap these messages in #ifdef. Since there is no console in "Release" I'm assuming that these messages are going nowhere.

My first question is: Is their any harm in leaving these debug lines in? I'm probably going to wrap them as well because they have no use in the "Release" version. But for the sake of argument, lets say I did. Is there a buffer that could fill up over time?

Second question is: Is the way I'm doing it right? I have seen three different ways of redirecting stdout to the console. I've heard that the above method is bad, and it could cause crashes. Why exactly? If I called fclose(stdout) before the program exits, would that be ok? What other methods are there out there? Pros and cons for each?

Third question is: How do I handle Control-C, the console being closed, etc... Right now both exit the program entirely. I've heard of GenerateConsoleCtrlEvent, but I'm not quite clear how to use it.

Luke
Topic archived. No new replies allowed.