8

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?

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Attic
  • 95
  • 1
  • 1
  • 4

3 Answers3

26

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");
Ryan Woodard
  • 491
  • 4
  • 7
10

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • When I've done this in the past, there were hoops I had to jump through for stdout to actually make it to the console window. By default it doesn't. – dash-tom-bang Mar 23 '10 at 17:09
  • @dash-tom-bang: Very true. The console, by default, won't redirect all standard output. I added a link to a page showing working code that demonstrates everything required. – Reed Copsey Mar 23 '10 at 17:19
  • 2
    A console shows up, but the data I put into std::cout does not show up there. – Attic Mar 23 '10 at 17:19
  • Wow, thats a lot of code for a task I thought would be easy. Thank you for the link, I will mark this answer as solution. – Attic Mar 23 '10 at 17:26
  • 1
    Yeah - it's kind of annoying - but it's a useful set of code to bookmark. I find that I've done this multiple times... – Reed Copsey Mar 23 '10 at 17:36
  • 1
    @Attic I know this thread is very old but look at the answer below. Its really nice and neat. – Sohaib Oct 10 '13 at 16:30
1

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

Dani
  • 444
  • 1
  • 3
  • 10