简体   繁体   中英

Is it possible to run a Windows console application together a with a Windows GUI application?

What I'm trying to do is have my Windows GUI application and a windows console application in the same program. So I have my GUI application showing up but I can output with std::cout to a console window. I would be outputting things like variable values etc.

This is possible, you have to keep in mind that Console link targets mean 32bit apps with console auto created, they are the same as GUI, thus you see that it is 'auto created' so that means you can do the same manually.

You can either make a console app with a GUI or a GUI app with a console, its up to you, personally I would make a console app with a GUI, because you can put the GUI into a separate thread even an ensure that the console is always there so it gives you an opportunity to catch fatal errors as well.

Here is a code snipped from one of my own project, it not precisely what you want to do but pretty similar

    if (!AttachConsole(ATTACH_PARENT_PROCESS) ||\
        GetLastError()==ERROR_NOT_SUPPORTED ||\
        GetLastError()==ERROR_INVALID_HANDLE)
    {
        if (!AllocConsole())
        {
// in this case we failed to alloc a console
// fatal error?
        }
    }
// at this point you already have a console or you created one
    SetConsoleTitle("caption");     // you can set the console caption if you want to
    freopen("CON","w",stdout);
    SetConsoleOutputCP(CP_UTF8);    // also this is needed when you want to use UTF8

AttachConsole is a special case when you want to start your app from cmd.exe or another console app, meaning that it will inherit the console, as far as I remember you cannot have more than one console so if your process inherits one ( like, you already have one ) you cant alloc a new one, the function will fail and nothing will happen. If you never ever run your app in this situation you dont need this part, altho I think this is much more complicated than this since Visual Studio can attach your console output into its log window meaning that it's in fact a nifty debug tool for 'free'. But I'm not sure it will work tho, never tried it.

AllocConsole may fail so you should also check for that case, after that section, you either have your own fresh console or inherited from another app somehow, and here comes the most important part! You have to reopen stdout so the system knows that stdout should go into this console.

Try the following to spawn a console, picked it up long ago from somewhere else but don't recall whom to give credit:

static const WORD MAX_CONSOLE_LINES = 500;
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
// allocate a console for this app
if( 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
    std::ios::sync_with_stdio();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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