Rasmi Ranjan Nayak
Rasmi Ranjan Nayak

Reputation: 11948

How to get console output data into command prompt?

I have a Windows based application. I made it to work in for both GUI Mode and Console Mode. In GUI mode or Console mode I am attaching a Console by using AttachConsole() to bring up output print statements to the 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. Suppose from command prompt, I am running it as

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

Then it brings up the another console, because of 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. But my requirement is to show the output in commandprompt instead of bringing up new console while executing from comamnd prompt.

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. Now my requirement is I need to disable 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(). Now want when I do,

d:\Project path > MyApp.exe consolemode **Enter** The output will come in comamnd prompt. like below

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

Please help me

Upvotes: 0

Views: 2583

Answers (2)

yiannis
yiannis

Reputation: 430

You should take the handle of the current running window and work on it so that you don't need 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;
}

Upvotes: 1

Not Submitted
Not Submitted

Reputation: 653

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. 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). 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). 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). 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).

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. This FreeConsole will not affect the parent process' console.

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:

    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/

Upvotes: 0

Related Questions