Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ Enable Console

I created an Empty Project in Visual C++, but now I need the Console to display debug output.

How can I enable the Console without recreating the project or show the output in the VS output window?

like image 906
Attic Avatar asked Mar 23 '10 17:03

Attic


3 Answers

Here's some code you can insert to get a console window in a GUI'd windows app that starts in WinMain. There are other ways to accomplish this but this is the most compact snippet I've found.

//Alloc Console
//print some stuff to the console
//make sure to include #include "stdio.h"
//note, you must use the #include <iostream>/ using namespace std
//to use the iostream... #incldue "iostream.h" didn't seem to work
//in my VC 6
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf("Debugging Window:\n");
like image 120
Ryan Woodard Avatar answered Oct 05 '22 20:10

Ryan Woodard


You can always call AllocConsole in code to create a console for your application, and attach it to the process. FreeConsole will remove the console, detaching the process from it, as well.

If you want all standard output stream data to go to the console, you need to also use SetStdHandle to redirect the output appropriately. Here is a page showing working code to do this full process, including allocating the console and redirecting the output.

like image 26
Reed Copsey Avatar answered Oct 05 '22 20:10

Reed Copsey


You can write to the vs output window with OutputDebugString. http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx

like image 45
Dani Avatar answered Oct 05 '22 19:10

Dani