Qt World Summit: Register Today!

AllocConsole() Not posting

  • So the situation is that my GUI posts to the allocated console perfectly when I open the application within the QT creator application, but when I try to launch the application as a separate .exe file in its own folder the console pops up but nothing gets posted.

    That's how I initialize the console (right after ui->setupui(this);) :

    // create a separate new console window
    // attach the new console to this application's process
    freopen("CON", "w", stdout);
    freopen("CON", "w", stderr);
    freopen("CON", "r", stdin);

    That's how i try posting to the console ( all of those ways work in QT creator and none of those work in the separate app)

    std::cout << "std::cout" << std::endl;

  • This post is deleted!

  • So the console starts working randomly sometimes.

    I think the problem is attaching the console to the app. I think so because when the console works right, I can close the console window and it will close the GUI automatically. But in situations when it does not post, I close the console and the GUI stays open.

  • Lifetime Qt Champion

    @Denis-Dolzhenko If you want to start your application outside of QtCreator then you need to deploy your app properly first. That means you need to crate a directory with your executable and all libs and Qt plug-ins which are used by your application.
    See http://doc.qt.io/qt-5/deployment.html

  • Here is everything I have in my final directory, all of my features worked properly except for the console thing


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The upload feature is currently broken, please follow this guide.

  • @SGaist Thanks, but I think that's not the problem, I figured something else out.

    So my assumption ( found it on https://stackoverflow.com/questions/432832/what-is-the-different-between-api-functions-allocconsole-and-attachconsole-1) is that console does not always attach if I launch the executable from the explorer.

    "When run from a command prompt, two message boxes containing a 1 are displayed, meaning both calls succeeded. When run from Start -> Run, the first box contains 1 and the second contains 0, meaning that only the first call succeeded. The second one fails because explorer.exe (which is the parent of a process launched from Start -> Run) doesn't have a console."

    I tested it and it works 100% of the times when I launch the app from the command prompt.

    Is there a way I can make this work without using the command prompt?

  • Lifetime Qt Champion

    Out of curiosity, why do you need that command prompt for ?

Log in to reply