代码之家  ›  专栏  ›  技术社区  ›  Nick Van Brunt

如何在Visual Studio 2010上查看Win32应用程序中的printf输出?

  •  17
  • Nick Van Brunt  · 技术社区  · 14 年前

    如何在 Win32 中的应用程序(使用WinMain输入) Visual Studio 2010 ?

    6 回复  |  直到 9 年前
        1
  •  26
  •   Peter Mortensen icecrime    10 年前

    严格回答您的问题,您可以使用 温基 OutputDebugString 功能。

    我写了一个简单的程序来演示如何操作。

    #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, "It works! - number: %d \n", number);
    
        OutputDebugString(str);
    
        return 0;
    }
    

    这个 输出调试 函数接受 LPCSTR 作为参数。我用过 sprintf_s 打印前格式化字符串。

    这将把结果打印到Visual Studio 2010输出窗口。

    希望有帮助!

        2
  •  14
  •   Hans Passant    14 年前

    你需要一个控制台窗口。到目前为止,获取链接器的最简单方法是更改链接器选项:project+属性、链接器、系统、subsystem=console。添加main()方法:

    int main() {
        return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);
    }
    
        3
  •  13
  •   Peter Mortensen icecrime    9 年前

    我知道我以前用 AllocConsole 函数,但我还记得它比我预期的要复杂一些。

    在alloconsole上快速搜索会产生一个明显的 Windows Developer Journal article 这似乎很重要。从那以后,下面的内容似乎和我记忆中的相似,虽然很模糊。

    void SetStdOutToNewConsole()
    {
        int hConHandle;
        long lStdHandle;
        FILE *fp;
    
        // Allocate a console for this app
        AllocConsole();
    
        // Redirect unbuffered STDOUT to the console
        lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
        hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
        fp = _fdopen(hConHandle, "w");
        *stdout = *fp;
    
        setvbuf(stdout, NULL, _IONBF, 0);
    }
    
        4
  •  9
  •   Quintin Willison    13 年前

    谢谢 托拉克 为了你的答案。这对我有很大帮助。

    我需要一个更大的回滚缓冲区,所以在查看 API functions . 在这里分享,以防它帮助其他人:

    void SetStdOutToNewConsole()
    {
        // allocate a console for this app
        AllocConsole();
    
        // redirect unbuffered STDOUT to the console
        HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        int fileDescriptor = _open_osfhandle((intptr_t)consoleHandle, _O_TEXT);
        FILE *fp = _fdopen( fileDescriptor, "w" );
        *stdout = *fp;
        setvbuf( stdout, NULL, _IONBF, 0 );
    
        // give the console window a nicer title
        SetConsoleTitle(L"Debug Output");
    
        // give the console window a bigger buffer size
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if ( GetConsoleScreenBufferInfo(consoleHandle, &csbi) )
        {
            COORD bufferSize;
            bufferSize.X = csbi.dwSize.X;
            bufferSize.Y = 9999;
            SetConsoleScreenBufferSize(consoleHandle, bufferSize);
        }
    }
    

    这会将后滚(屏幕缓冲区)高度增加到9999行。

    在Windows XP和Windows 7上测试。

        5
  •  8
  •   colin lamarre    9 年前

    另一种不需要更改现有的printf,也不需要打印到vs输出窗口的方法如下:

    #define printf printf2
    
    int __cdecl printf2(const char *format, ...)
    {
        char str[1024];
    
        va_list argptr;
        va_start(argptr, format);
        int ret = vsnprintf(str, sizeof(str), format, argptr);
        va_end(argptr);
    
        OutputDebugStringA(str);
    
        return ret;
    }
    
    ...
    
    printf("remains %s", "the same");
    
        6
  •  2
  •   coffeebean    14 年前

    Here is a page that will tell you how to do this, including sample code.

    必须使用alloconsole()创建控制台窗口,然后将C标准文件句柄与新控制台窗口的句柄关联。