2010-06-09 197 views
15

如何在Visual Studio 2010Win32應用程序(使用WinMain輸入)中查看printf輸出?如何在Visual Studio 2010上查看Win32應用程序中的printf輸出?

+0

你想從應用程序中打開一個單獨的控制檯窗口,還是你想將其顯示在主應用程序窗口的控件上?或將其記錄到文件? – 2010-06-09 19:45:14

+1

其實我希望能夠像xcode中的控制檯窗口那樣可以看到控制檯輸出而不必更改任何代碼。顯示stdout的日誌也可以。 – 2010-06-09 20:05:18

回答

23

嚴格回答你的問題,你可以在Win32應用程序在Visual Studio 2010中的WINBASE.HOutputDebugString功能使用類似printf函數。

我寫了一個簡單的程序,顯示如何做到這一點。

#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; 
} 

OutputDebugString函數接受一個LPCSTR作爲參數。打印前,我使用sprintf_s來格式化字符串。

這會將結果打印到視覺 工作室  2010輸出窗口。

我希望它有幫助!

+0

很明顯,256只是例子,但我喜歡這種方法。絕對是我的想法。 – 2011-06-30 18:19:16

+0

當然,只是一個例子。爲了方便起見,你可以把它包裝在一個類/函數中。很高興幫助!乾杯! – rbento 2011-07-05 21:34:47

+0

'wchar_t str [256]; wsprintf(str,L「It works! - number:%d \ n」,number); OutputDebugString(str);' – 2016-07-07 20:23:09

12

我知道我在過去使用AllocConsole函數完成了這項工作,但我也記得它比我預期的稍微複雜一點。

快速谷歌搜索AllocConsole產生什麼顯然是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); 
} 
+0

由於某些原因,這不適用於彩色輸出 – paulm 2016-01-01 14:06:37

11

您需要一個控制檯窗口。到目前爲止最簡單的方法是更改​​鏈接器選項:項目+屬性,鏈接器,系統,子系統=控制檯。添加一個main()方法:

int main() { 
    return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW); 
} 
+0

'_tWinMain'是Win32應用程序的主要功能,而不是「WinMain」 – 2014-01-28 03:57:50

9

感謝torak你的答案。這對我幫助很大。

我需要一個更大的滾動回緩衝區,所以在看了一下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

+2

爲了節省對像我這樣的剪貼者的查找,還需要包含函數和標誌定義 – 2017-02-27 04:54:13

6

另一種方式,也不會要求改變現有的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"); 
+1

你是個天才! – Skynight 2017-05-30 04:35:09

相關問題