How to Print to the Debug Output Window in a Win32 App

How do I print to the debug output window in a Win32 app?

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.");

Printing debug output in Windows programs

I normally use OutputDebugString(), the API is here. While running the application you can then view the output of this function with DebugView from SysInternals or in the Visual Studio output window while debugging.

Output to console/Debug window in Win32 Application on MVC++ 2010

You're looking for the OutputDebugString function.

When running under the debugger (e.g., in Visual Studio), the arguments to this function are automatically redirected to the Output window.

how to log in win32 c++ to the visual studio output window?

I think you're looking for OutputDebugString().

How to write to the Output window in Visual Studio?

OutputDebugString function will do it.

example code

    void CClass::Output(const char* szFormat, ...)
{
char szBuff[1024];
va_list arg;
va_start(arg, szFormat);
_vsnprintf(szBuff, sizeof(szBuff), szFormat, arg);
va_end(arg);

OutputDebugString(szBuff);
}

Win32 Application Console Output?

OutputDebugString.

I assume that you want to write to the debug console, since that's what your screenshot shows. OutputDebugString is a nop when no debugger is attached, but it allows you to log whatever you want to the debugger's output.

OutputDebugStringW(L"This will go to the output.\n");

// or

OutputDebugString("This will go to the output.\n");

Is it possible to debug in console in a Win32 C++ Application in Visual Studio?

You can do this via the AttachConsole and/or AllocConsole functions of the Windows Console API (see https://learn.microsoft.com/en-us/windows/console/console-functions). You can also simply create a Console Application in your IDE, and call your GUI code from that application.

Output to console from a Win32 GUI application on Windows 10

Based on the accepted answer from ProXicT link with a few modifications. The following code works for std::cout. The other methods won't work on 64bit with Visual Studio 2015:

#include <iostream>
#include <cstdio>
#include <fstream>

#include <Windows.h>

// For debugging
#include <io.h>
#include <fcntl.h>

#define UNUSED(x) (void)(x) // Unused param (C compatible - not applicable to expressions)

class outbuf : public std::streambuf {
public:
outbuf() {
setp(0, 0);
}

virtual int_type overflow(int_type c = traits_type::eof()) {
return fputc(c, stdout) == EOF ? traits_type::eof() : c;
}
};

int CALLBACK
WinMain (HINSTANCE hInstance,
HINSTANCE /*hPrevInst*/, // Unused param (C++ only)
LPSTR lpCmdLine,
int (nShowCmd))
{
UNUSED(hInstance);
// UNUSED(hPrevInst);
UNUSED(lpCmdLine);
UNUSED(nShowCmd); // This param is used

// create the console
if (AllocConsole()) {
FILE* pCout;
freopen_s(&pCout, "CONOUT$", "w", stdout);
SetConsoleTitle(L"Debug Console");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
}

// set std::cout to use my custom streambuf
outbuf ob;
std::streambuf *sb = std::cout.rdbuf(&ob);

// do some work here
printf("Hello!\n");

std::cout << "nShowCmd = " << nShowCmd << std::endl;
std::cout << "Now making my first Windows window!" << std::endl;

// make sure to restore the original so we don't get a crash on close!
std::cout.rdbuf(sb);

MessageBoxW (NULL,
L"Hello World!",
L"hello",
MB_OK | MB_ICONINFORMATION);

return 0;
}

EDIT:

The same stuff as above but with colors for completeness:

#include <iostream>
#include <cstdio>
#include <fstream>

#include <Windows.h>

// For debugging
#include <io.h>
#include <fcntl.h>

#define UNUSED(x) (void)(x) // Unused param (C compatible - not applicable to expressions)

class outbuf : public std::streambuf {
public:
outbuf() {
setp(0, 0);
}

virtual int_type overflow(int_type c = traits_type::eof()) {
return fputc(c, stdout) == EOF ? traits_type::eof() : c;
}
};

int CALLBACK
WinMain (HINSTANCE hInstance,
HINSTANCE /*hPrevInst*/, // Unused param (C++ only)
LPSTR lpCmdLine,
int (nShowCmd))
{
UNUSED(hInstance);
// UNUSED(hPrevInst);
UNUSED(lpCmdLine);
UNUSED(nShowCmd); // This param is used

// create the console
if (AllocConsole()) {
FILE* pCout;
freopen_s(&pCout, "CONOUT$", "w", stdout);
SetConsoleTitle(L"Debug Console");

}

// set std::cout to use my custom streambuf
outbuf ob;
std::streambuf *sb = std::cout.rdbuf(&ob);

// do some work here

printf("Hello!\n");

HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD defaultConsoleTextAttributes;
GetConsoleScreenBufferInfo(stdHandle, &consoleInfo);
defaultConsoleTextAttributes = consoleInfo.wAttributes;
WORD currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "nShowCmd = " << nShowCmd << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;

// make sure to restore the original so we don't get a crash on close!
std::cout.rdbuf(sb);

Sleep(5000);
ShowWindow(GetConsoleWindow(), SW_HIDE);
FreeConsole();

MessageBoxW (NULL,
L"Hello World!",
L"hello",
MB_OK | MB_ICONINFORMATION);

return 0;
}

All that remains now is to make it into a complete logging class.

Printing output on the Output Window in Visual C++ IDE

You can use OutputDebugString("..."); to print to the Output window of Visual Studio. You have to #include <windows.h> though.



Related Topics



Leave a reply



Submit