Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view printf output in a Win32 application on Visual Studio 2010?

How can you view printf output in a Win32 application (entering with a WinMain) in Visual Studio 2010?

like image 957
Nick Van Brunt Avatar asked Jun 09 '10 19:06

Nick Van Brunt


People also ask

How do I view the output window in Visual Studio?

To display the Output window whenever you build a project, in the Options dialog box, on the Projects and Solutions > General page, select Show Output window when build starts.

How do I write to the Output window in Visual Studio?

You can write run-time messages to the Output window using the Debug class or the Trace class, which are part of the System. Diagnostics class library. Use the Debug class if you only want output in the Debug version of your program. Use the Trace class if you want output in both the Debug and Release versions.


2 Answers

Edit 2021, Visual Studio 2019

To write debug messages to the Output window use the OutputDebugStringA from debugapi.h (include windows.h)

test.c

#include <windows.h> #include <stdio.h>  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdShow, int nCmdShow) {     int number = 10;     char str[256];     sprintf_s(str, sizeof(str), "It works! - number: %d \n", number);      OutputDebugStringA(str);      return 0; } 

Tested on Visual Studio 2019, Debug / x64.

Or alternatively utilize my drop-in header file.

like image 179
rbento Avatar answered Sep 21 '22 18:09

rbento


You'll need a console window. By far the easiest way to get one is to change a linker option: Project + Properties, Linker, System, SubSystem = Console. Add a main() method:

int main() {     return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW); } 
like image 32
Hans Passant Avatar answered Sep 17 '22 18:09

Hans Passant