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

后端 未结 7 1357
梦如初夏
梦如初夏 2021-02-01 05:48

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

7条回答
  •  野的像风
    2021-02-01 06:02

    Strictly answering your question, you may use printf-like functions in a Win32 application in Visual Studio 2010 using the winbase.h OutputDebugString function.

    I wrote a simple program that shows how to do it.

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

    The OutputDebugString function takes an LPCSTR as a parameter. I used the sprintf_s to format the string before printing.

    This would print the result to the Visual Studio 2010 output window.

    I hope it helps!

提交回复
热议问题