Write debug output to console window | CodeGuru

Write debug output to console window

Environment: any MSVC It’s very common that we have to test our program on another machine where the Visual C++ IDE is not installed. So there will be no debug window for output. Logging into a file or keep popping up MessageBoxs are not very practical ways. An easy solution is to show the output […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 11, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: any MSVC

It’s very common that we have to test our program on another machine
where the Visual C++ IDE is not installed. So there will be no debug window for output.
Logging into a file or keep popping up MessageBoxs are not very practical ways.
An easy solution is to show the output strings in a console window.

Put the following code at the beginning of your app file or in a separate file.

#ifdef _DEBUG
    FILE* __fStdOut = NULL;
    HANDLE __hStdOut = NULL;
#endif
// width and height is the size of console window, if you specify fname,
// the output will also be writton to this file. The file pointer is automatically closed
// when you close the app
void startConsoleWin(int width=80, int height=25, char* fname = NULL);
void startConsoleWin(int width, int height, char* fname)
{
#ifdef _DEBUG
	AllocConsole();
	SetConsoleTitle(“Debug Window”);
	__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD co = {width,height};
	SetConsoleScreenBufferSize(__hStdOut, co);
	if(fname)
		__fStdOut = fopen(fname, “w”);
#endif
}
// Use wprintf like TRACE0, TRACE1, … (The arguments are the same as printf)
int wprintf(char *fmt, …)
{
#ifdef _DEBUG
	char s[300];
	va_list argptr;
	int cnt;
	va_start(argptr, fmt);
	cnt = vsprintf(s, fmt, argptr);
	va_end(argptr);
	DWORD cCharsWritten;
	if(__hStdOut)
		WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);
	if(__fStdOut)
		fprintf(__fStdOut, s);
	return(cnt);
#else
	return 0;
#endif
}

In CWinApp::InitInstance() call startConsoleWin() and a console window will popup. Then use wprintf at
places where you used to use TRACE. The output strings will then appear in the console window.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.