C语言 如何在 Visual Studio 2010 上的 Win32 应用程序中查看 printf 输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3009042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to view printf output in a Win32 application on Visual Studio 2010?
提问by Nick Van Brunt
How can you view printf output in a Win32application (entering with a WinMain) in Visual Studio 2010?
如何在Visual Studio 2010的Win32应用程序(使用 WinMain 输入)中查看 printf 输出?
回答by rbento
Strictly answering your question, you may use printf-like functions in a Win32 application in Visual Studio 2010 using the winbase.hOutputDebugStringfunction.
严格回答您的问题,您可以使用winbase.hOutputDebugString函数在 Visual Studio 2010 中的 Win32 应用程序中使用类似 printf 的函数。
I wrote a simple program that shows how to do it.
我写了一个简单的程序来展示如何做到这一点。
#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;
}
The OutputDebugStringfunction takes an LPCSTRas a parameter. I used the sprintf_sto format the string before printing.
该OutputDebugString函数将 anLPCSTR作为参数。我sprintf_s在打印前使用来格式化字符串。
This would print the result to the Visual Studio 2010 output window.
这会将结果打印到 Visual Studio 2010 输出窗口。
I hope it helps!
我希望它有帮助!
回答by torak
I know that I have done this in the past using the AllocConsolefunction, but I also recall that it was just a little trickier than I expected.
我知道我过去曾使用AllocConsole函数完成此操作,但我也记得它只是比我预期的要复杂一些。
A quick Google search on AllocConsole yields what is apparently a Windows Developer Journal articlethat seems relevant. From there, the following seems similar to what I recall, vague as it is.
在 AllocConsole 上进行快速 Google 搜索,可以找到显然是相关的Windows Developer Journal 文章。从那里开始,以下内容似乎与我所记得的相似,但仍然含糊不清。
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);
}
回答by Hans Passant
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:
您将需要一个控制台窗口。到目前为止,最简单的方法是更改链接器选项:项目 + 属性、链接器、系统、子系统 = 控制台。添加一个 main() 方法:
int main() {
return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);
}
回答by Quintin Willison
Thanks torakfor your answer. It helped me a lot.
感谢托拉克的回答。这对我帮助很大。
I needed a bigger scroll back buffer so made a few additions after taking a look at the API functions. Shared here in case it helps anybody else:
我需要一个更大的回滚缓冲区,所以在查看了API 函数后做了一些补充。在这里分享,以防它对其他人有帮助:
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);
}
}
This increases the scroll back (screen buffer) height to 9999 lines.
这会将回滚(屏幕缓冲区)高度增加到 9999 行。
Tested on Windows XP and Windows 7.
在 Windows XP 和 Windows 7 上测试。
回答by colin lamarre
Another way which wouldn't require changing existing printf's and also print to VS output window would go something like this:
另一种不需要更改现有 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");
回答by coffeebean
Here is a page that will tell you how to do this, including sample code.
You must create a console window using AllocConsole(), then associate the C standard file handles to the HANDLEs of the new console window.
您必须使用 AllocConsole() 创建一个控制台窗口,然后将 C 标准文件句柄关联到新控制台窗口的句柄。
回答by User
For MinGW use "_A_SYSTEM" instead "_O_TEXT". So ported Quintin Willisonanswer is as follows:
对于 MinGW,使用“_A_SYSTEM”代替“_O_TEXT”。所以移植Quintin Willison 的答案如下:
#include <io.h>
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, _A_SYSTEM);
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);
}
}

