Update: my app is c++ qt app.
Options I have found:
To have two separate apps: gui one and console one. Honestly, I would prefer not too. As I have appimage, rpm, deb, and qt installer framework working on my executable and I will have to extend their logics.
To have console app, i.e. built without
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE $<CONFIG:Release>)
. And when I don't supply--no-ui
option, to callFreeConsole()
. This have some drawbacks.First, the black console quickly flashing before being freed by
FreeConsole()
. Secondly, I have found the menacing comment "Be warned that FreeConsole has incredibly dangerous behaviour on Windows 8: https://stackoverflow.com/questions/12676312/freeconsole-behaviour-on-windows-8" by the link Console output in a Qt GUI app?.To have gui app, i.e. built with
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE $<CONFIG:Release>)
. But when I supply--no-ui
, to call
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
#endif
This works but the output is intermixed with cmd's output and it's impossible to get stdin work. I always have lot of not recognized as an internal or external command
, as described here Using istream (std cin): prevent "[input] is not recognized as ..." on Windows.
I also found this post https://forum.qt.io/topic/56484/solved-attach-console-to-gui-application-on-windows/5 where it's suggested to allocate a new console. I.e., I would have a gui version of my app and would use
AllocConsole() + AttachConsole()
. This variant does not help me, as I would prefer to communicate with the initial cmd and not to open a new console. Also, it flashes a console when I run my google tests and they reach this code on Windows.I asked the question FreeConsole() does not detach application from cmd and there I got an answer https://stackoverflow.com/a/73971983/4781940 but I still cannot get it.
I would be any grateful for any ideas. It would be great if they were with source code (I am very badly acquainted with WinApi). Maybe, somebody could explain to me the idea from the paragraph 5's link. Also, I do not insist on the solution to have stdin working. It would be great at least to have stdout working well. Thank you.
Update: if there is no better options, maybe, you could tell which method of the above-mentioned would you choose and why.