简体   繁体   English

如何使控制台输出数据进入命令提示符?

[英]How to get console output data into command prompt?

I have a Windows based application. 我有一个基于Windows的应用程序。 I made it to work in for both GUI Mode and Console Mode . 我使它可以同时在GUI ModeConsole Mode In GUI mode or Console mode I am attaching a Console by using AttachConsole() to bring up output print statements to the console. GUI modeConsole mode我通过使用AttachConsole()Console的输出打印语句附加到控制台。 Now the challenge is, I don't need new console to come up while I am using it in console mode or from Command prompt . 现在的挑战是,在console mode或从Command prompt console mode使用它时,不需要新的控制台。 Suppose from command prompt, I am running it as 假设从命令提示符下运行

d:\\Project path > MyApp.exe consolemode **Enter** d:\\Project path > MyApp.exe consolemode **Enter**

Then it brings up the another console, because of Attachconsole() . 然后,由于Attachconsole() ,它会打开另一个控制台。 Now when I disable the AttachConsole() it does not bring up the new console and it does not show the output in command prompt too. 现在,当我禁用AttachConsole()它不会启动新控制台,并且也不会在命令提示符下显示输出。 But my requirement is to show the output in commandprompt instead of bringing up new console while executing from comamnd prompt. 但是我的要求是在commandprompt显示输出,而不是在从comamnd提示符下执行时调出新控制台。

Myapp.cpp
Winmain()
{
....
...
AttachConsole();
cout << "Console Attached \n";
// Some more output
}

So, when i run the myapp.exe from command promt d:\\Project path > MyApp.exe consolemode **Enter** it attaches a new console and prints the output in new console window. 因此,当我从命令promt d:\\Project path > MyApp.exe consolemode **Enter**运行myapp.exe ,它将附加一个new console并在新控制台窗口中打印输出。 Now my requirement is I need to disable AttachConsole(); 现在我的要求是我需要禁用AttachConsole(); and wanted to see the output in the command prompt. 并希望在命令提示符下查看输出。

Myapp.cpp
Winmain()
{
....
...
//AttachConsole(); //Now I an disabling console
cout << "Console Attached \n";
// Some more output
}

If you look at the code above I have disabled the AttachConsole() . 如果您看上面的代码,我已禁用AttachConsole() Now want when I do, 现在想当我做

d:\\Project path > MyApp.exe consolemode **Enter** The output will come in comamnd prompt. d:\\Project path > MyApp.exe consolemode **Enter**输出将以d:\\Project path > MyApp.exe consolemode提示。 like below 像下面

d:\Project path > MyApp.exe consolemode **Enter**
Console Attached
....
...
d:\Project path >

Please help me 请帮我

You should take the handle of the current running window and work on it so that you don't need allocconsole. 您应该处理当前正在运行的窗口并对其进行处理,以使您不需要allocconsole。 Also you must find where the cursor is and specify that your output should be written there. 另外,您必须找到光标所在的位置,并指定将输出写入该位置。

HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbi; 

void cp( HANDLE hConsole,wchar_t* output )
{


    DWORD cCharsWritten; 
    COORD  crCurr;
    GetConsoleScreenBufferInfo(hStdout, &csbi);
    crCurr = csbi.dwCursorPosition;

    std::wstring ss;
    ss=output;

    if( !WriteConsoleOutputCharacter( hConsole,       
                                ss.c_str(),     
                                (DWORD)ss.length(), 
                                crCurr,     
                                &cCharsWritten ))
    {
      return;
    }

}

int main( void )
{

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout, &csbi);

    function(hStdout,L"string");
    return 0;
}

A process can have only one console. 一个进程只能有一个控制台。 So if there already is a console then AllocConsole will fail, but AttachConsole will succeed if you're not already attached to the parent process' console. 因此,如果已经有一个控制台,则AllocConsole将会失败,但是如果您尚未附加到父进程的控制台,则AttachConsole将成功。 If you run from a command prompt, you will already be attached to the parent console, which will be cmd.exe, and AttachConsole will also fail (unless you call FreeConsole first). 如果从命令提示符运行,您将已经附加到父控制台(即cmd.exe),并且AttachConsole也将失败(除非您先调用FreeConsole)。 If there is no console ("GUI mode"), then AllocConsole will succeed (and so will AttachConsole if you know the PID of a process that has a console you can attach to and you have sufficient access rights). 如果没有控制台(“ GUI模式”),则AllocConsole将成功(如果您知道具有可以附加到控制台的进程的PID,并且具有足够的访问权限,则AllConConsole也将成功)。 So you just need to first try Allocating, if that fails then call AttachConsole(-1), and if that fails then call AllocConsole (or FreeConsole then AttachConsole). 因此,您只需要先尝试分配,如果失败,则调用AttachConsole(-1),如果失败,则调用AllocConsole(或先调用FreeConsole,再调用AttachConsole)。 Note: you don't have to also call AttachConsole if you create the console via AllocConsole (AttachConsole is only used to attach to a different console, usually the console of a different process). 注意:如果通过AllocConsole创建控制台,则不必也调用AttachConsole(AttachConsole仅用于附加到其他控制台,通常是不同进程的控制台)。

If you don't want to use the cmd.exe console in "console mode", you can call FreeConsole (after determining there is already a console by seeing AllocConsole fail), then AllocConsole will succeed. 如果您不想在“控制台模式”下使用cmd.exe控制台,则可以调用FreeConsole(在通过查看AllocConsole失败确定控制台之后),AllocConsole将成功。 This FreeConsole will not affect the parent process' console. 这个FreeConsole不会影响父进程的控制台。

If you want to use standard output functions such as printf or cout to write to the console you allocated with AllocConsole, or scanf to read from the console when running in "GUI" mode you must explicitly set the standard handles as follows: 如果要使用标准输出功能(例如printf或cout)写入使用AllocConsole分配的控制台,或者在“ GUI”模式下运行时使用scanf从控制台读取控制台,则必须显式设置标准句柄,如下所示:

    freopen("CON", "w", stdout);
    freopen("CON", "w", stderr);
    freopen("CON", "r", stdin);

Not too long ago I wrote a blog post on how to do this. 不久前,我写了一篇有关如何执行此操作的博客文章。 I realize it is frowned upon to post links, but I do think you you will find it useful. 我知道发布链接是不受欢迎的,但是我认为您会发现它很有用。 There's a Visual Studio solution for a complete sample app that you can download: http://www.windowsinspired.com/how-to-add-a-debugging-console-to-a-windows-gui-application/ 您可以下载完整的示例应用程序的Visual Studio解决方案: http : //www.windowsinspired.com/how-to-add-a-debugging-console-to-a-windows-gui-application/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM