views:

652

answers:

3

I've got a win32 project that I've loaded into Visual Studio 2005. I'd like to be able to print things to the Visual Studio output window, but I can't for the life of me work out how. I've tried 'printf' and 'cout <<' but my messages stay stubbornly unprinted.

Is there some sort of special way to print to the Visual Studio output window?

+9  A: 

You can use OutputDebugString. OutputDebugString is a macro that depending on your build options either maps to OutputDebugStringA(char const*) or OutputDebugStringW(wchar_t const*). In the later case you will have to supply a wide character string to the function. To create a wide character literal you can use the L prefix:

OutputDebugStringW(L"My output string.");

Normally you will use the macro version together with the _T macro like this:

OutputDebugString(_T("My output string."));

If you project is configured to build for UNICODE it will expand into:

OutputDebugStringW(L"My output string.");

If you are not building for UNICODE it will expand into:

OutputDebugStringA("My output string.");
Martin Liversage
Perfect! Thanks. For completeness though, it turned out I had to do this: OutputDebugString(TEXT("Hello console world")); .. presumably due to some sort of unicode-related build option.
izb
@izb: I expanded my answer a bit to include information about the two versions of the function.
Martin Liversage
+1. In general, you use the macro version and enclose the string in `_T` (which is the same as `TEXT` only shorter).
avakar
note that you will find it useful to have debugview from sysinternals. This allows you to see the ODS output even if Visual Studio is not running (or even installed) on the box
pm100
+4  A: 

To print to the "real" console, you need to make it visible by using the linker flag /SUBSYSTEM:CONSOLE. The extra console window is annoying, but for debugging purposes it's very valuable.

OutputDebugString prints to the debugger output when running inside the debugger.

Ringding
You can also allocate your own console using AllocConsole()
Billy ONeal
+1  A: 

Your Win32 project is likely a GUI project, not a console project. This causes a difference in the executable header. As a result, your GUI project will be responsible for opening its own window. That may be a console window, though. Call AllocConsole() to create it, and use the Win32 console functions to write to it.

MSalters