如何使用Windows程序在C++中获取控制台输出?

问题描述:

如果我有一个本地C++ Windows程序(即入口点是WinMain),如何查看控制台函数(如std :: cout)的输出?如何使用Windows程序在C++中获取控制台输出?

+1

您是否正在尝试读取您的输出或其他应用程序输出? – 2008-10-10 15:26:09

由于没有控制台窗口,这是 不可能 困难。 (每天学点新东西 - 我从来不知道控制台的功能!)

是否有可能替换您的输出呼叫?我经常使用TRACE或OutputDebugString将信息发送到Visual Studio输出窗口。

+2

实际上,使用WinMain()入口点而不是main()可以获得控制台窗口。 – 2008-10-10 15:30:24

+0

@ChrisCharabaruk:调用[AllocConsole](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681944.aspx)并不十分棘手。这在[创建控制台](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682528.aspx)中有解释。此外,用户提供的入口点的名称不控制控制台的创建。这是子系统的PE头条目(`WINDOWS`与`CONSOLE`)。 Visual Studio使用该链接器设置来控制CRT的启动代码调用哪个用户提供的入口点。 – IInspectable 2017-02-20 09:50:24

不要在此引用我,但Win32 console API可能是您正在寻找的。但是,如果您只是为了调试目的而执行此操作,则可能更愿意运行DebugView并调用DbgPrint函数。

这当然假设您的应用程序要发送控制台输出,而不是从另一个应用程序读取它。在这种情况下,管道可能是你的朋友。

+0

根据“DbgPrint”的链接,它认为“只有内核模式驱动程序可以调用DbgPrint例程”。所以可能不是大多数人想要的 – Paladin 2016-05-23 07:26:40

+0

呵呵。那么,我知道有一个用户模式函数用于记录将出现在DebugView中的消息。我认为DbgPrint是它,但显然不是。 – 2016-05-24 14:11:45

结帐Adding Console I/O to a Win32 GUI App。这可能会帮助你做你想做的事。

如果您没有或无法修改代码,请尝试使用建议here将控制台输出重定向到文件。


编辑:这里有点线程巫术。我在9年前首先回答了这个问题,就在SO初期,在非链接答案的(良好)政策生效之前。我会重新发布原始文章中的代码,希望能够弥补过去的罪孽。

guicon.cpp - 甲控制台重定向功能

#include <windows.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <io.h> 
#include <iostream> 
#include <fstream> 
#ifndef _USE_OLD_IOSTREAMS 
using namespace std; 
#endif 
// maximum mumber of lines the output console should have 
static const WORD MAX_CONSOLE_LINES = 500; 
#ifdef _DEBUG 
void RedirectIOToConsole() 
{ 
    int hConHandle; 
    long lStdHandle; 
    CONSOLE_SCREEN_BUFFER_INFO coninfo; 
    FILE *fp; 

    // allocate a console for this app 
    AllocConsole(); 

    // set the screen buffer to be big enough to let us scroll text 
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); 
    coninfo.dwSize.Y = MAX_CONSOLE_LINES; 
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); 

    // 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); 

    // redirect unbuffered STDIN to the console 
    lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); 
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); 
    fp = _fdopen(hConHandle, "r"); 
    *stdin = *fp; 
    setvbuf(stdin, NULL, _IONBF, 0); 

    // redirect unbuffered STDERR to the console 
    lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); 
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); 
    fp = _fdopen(hConHandle, "w"); 
    *stderr = *fp; 
    setvbuf(stderr, NULL, _IONBF, 0); 

    // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog 
    // point to console as well 
    ios::sync_with_stdio(); 
} 

#endif 
//End of File 

guicon.h - 接口到控制台重定向功能

#ifndef __GUICON_H__ 
#define __GUICON_H__ 
#ifdef _DEBUG 

void RedirectIOToConsole(); 

#endif 
#endif 

// End of File 

TEST.CPP - 演示控制台重定向

#include <windows.h> 
#include <iostream> 
#include <fstream> 
#include <conio.h> 
#include <stdio.h> 
#ifndef _USE_OLD_OSTREAMS 
using namespace std; 
#endif 
#include "guicon.h" 


#include <crtdbg.h> 

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) 
{ 
    #ifdef _DEBUG 
    RedirectIOToConsole(); 
    #endif 
    int iVar; 

    // test stdio 
    fprintf(stdout, "Test output to stdout\n"); 
    fprintf(stderr, "Test output to stderr\n"); 
    fprintf(stdout, "Enter an integer to test stdin: "); 
    scanf("%d", &iVar); 
    printf("You entered %d\n", iVar); 

    //test iostreams 
    cout << "Test output to cout" << endl; 
    cerr << "Test output to cerr" << endl; 
    clog << "Test output to clog" << endl; 
    cout << "Enter an integer to test cin: "; 
    cin >> iVar; 
    cout << "You entered " << iVar << endl; 
    #ifndef _USE_OLD_IOSTREAMS 

    // test wide iostreams 
    wcout << L"Test output to wcout" << endl; 
    wcerr << L"Test output to wcerr" << endl; 
    wclog << L"Test output to wclog" << endl; 
    wcout << L"Enter an integer to test wcin: "; 
    wcin >> iVar; 
    wcout << L"You entered " << iVar << endl; 
    #endif 

    // test CrtDbg output 
    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); 
    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); 
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); 
    _RPT0(_CRT_WARN, "This is testing _CRT_WARN output\n"); 
    _RPT0(_CRT_ERROR, "This is testing _CRT_ERROR output\n"); 
    _ASSERT(0 && "testing _ASSERT"); 
    _ASSERTE(0 && "testing _ASSERTE"); 
    Sleep(2000); 
    return 0; 
} 

//End of File 

您也可以重新打开cout和cerr流以输出到文件。以下应为这方面的工作:

#include <iostream> 
#include <fstream> 

int main() 
{ 
    std::ofstream file; 
    file.open ("cout.txt"); 
    std::streambuf* sbuf = std::cout.rdbuf(); 
    std::cout.rdbuf(file.rdbuf()); 
    //cout is now pointing to a file 
    return 0; 
} 

创建一个管道,执行程序控制台的CreateProcess()与ReadFile的阅读(),或在控制台的WriteFile()写入

HANDLE hRead ; // ConsoleStdInput 
    HANDLE hWrite; // ConsoleStdOutput and ConsoleStdError 

    STARTUPINFO   stiConsole; 
    SECURITY_ATTRIBUTES segConsole; 
    PROCESS_INFORMATION priConsole; 

    segConsole.nLength = sizeof(segConsole); 
    segConsole.lpSecurityDescriptor = NULL; 
    segConsole.bInheritHandle = TRUE; 

if(CreatePipe(&hRead,&hWrite,&segConsole,0)) 
{ 

    FillMemory(&stiConsole,sizeof(stiConsole),0); 
    stiConsole.cb = sizeof(stiConsole); 
GetStartupInfo(&stiConsole); 
stiConsole.hStdOutput = hWrite; 
stiConsole.hStdError = hWrite; 
stiConsole.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 
stiConsole.wShowWindow = SW_HIDE; // execute hide 

    if(CreateProcess(NULL, "c:\\teste.exe",NULL,NULL,TRUE,NULL, 
     NULL,NULL,&stiConsole,&priConsole) == TRUE) 
    { 
     //readfile and/or writefile 
}  

}

转到项目>项目属性>链接器>系统,并在右侧窗格中将SubSystems选项设置为控制台(/ SUBSYSTEM:CONSOLE)

然后编译您的程序并从控制台运行它,查看您的命令提示是否显示您的输出。

如果要将程序的输出发送到文件或管道,例如

myprogram.exe > file.txt 
myprogram.exe | anotherprogram.exe 

或者您正在从另一个程序调用您的程序并通过管道捕获其输出,那么您无需更改任何内容。即使入口点是WinMain,它也会正常工作。

但是,如果您在控制台或Visual Studio中运行程序,则输出不会显示在控制台或Visual Studio的“输出”窗口中。如果你想看到输出“生活”,然后尝试其他答案之一。

基本上,这意味着标准输出就像控制台应用程序一样工作,但它并未连接到正在运行应用程序的控制台,似乎没有简单的方法来执行此操作(所有其他这里给出的解决方案将输出连接到新的控制台窗口,当您运行应用程序时,即使从另一个控制台也会弹出一个新窗口。

结合使用luke's answerRoger's answer here在我的Windows桌面应用程序项目中为我工作。

void RedirectIOToConsole() { 

    //Create a console for this application 
    AllocConsole(); 

    // Get STDOUT handle 
    HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
    int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT); 
    FILE *COutputHandle = _fdopen(SystemOutput, "w"); 

    // Get STDERR handle 
    HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE); 
    int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT); 
    FILE *CErrorHandle = _fdopen(SystemError, "w"); 

    // Get STDIN handle 
    HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE); 
    int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT); 
    FILE *CInputHandle = _fdopen(SystemInput, "r"); 

    //make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well 
    ios::sync_with_stdio(true); 

    // Redirect the CRT standard input, output, and error handles to the console 
    freopen_s(&CInputHandle, "CONIN$", "r", stdin); 
    freopen_s(&COutputHandle, "CONOUT$", "w", stdout); 
    freopen_s(&CErrorHandle, "CONOUT$", "w", stderr); 

    //Clear the error state for each of the C++ standard stream objects. We need to do this, as 
    //attempts to access the standard streams before they refer to a valid target will cause the 
    //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems 
    //to always occur during startup regardless of whether anything has been read from or written to 
    //the console or not. 
    std::wcout.clear(); 
    std::cout.clear(); 
    std::wcerr.clear(); 
    std::cerr.clear(); 
    std::wcin.clear(); 
    std::cin.clear(); 

}