0

Update: my app is c++ qt app.

Options I have found:

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

  2. 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 call FreeConsole(). 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?.

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

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

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

JenyaKh
  • 2,040
  • 17
  • 25
  • Why not have two apps - if the "back end" is separated out nicely, how it gets called is flexible. – doctorlove Oct 07 '22 at 13:30
  • Well, maybe I will end up with having two apps. As I mentioned above, I will have to extend lots of infrastructure around. – JenyaKh Oct 07 '22 at 13:33
  • 1
    You can have a GUI on top of a console app. – Jesper Juhl Oct 07 '22 at 13:37
  • 1
    In #2 why are you calling FreeConsole(). I have a fully working console for debugging purposes in my Qt GUI applications using CMake + Visual Studio. I don't call FreeConsole() – drescherjm Oct 07 '22 at 13:38
  • Because in this case I have a black console hanging next to my qt app's window – JenyaKh Oct 07 '22 at 13:43
  • Maybe you can hide the window if you want it to not be there under certain conditions. – drescherjm Oct 07 '22 at 13:44
  • @JenyaKh Ask yourself how other programs are able to function as GUI and console. There are plenty out there, where you have a basic console application, and a fancy GUI user-interface. More than likely, they followed the [Model-View-Controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) design pattern. Did you do that, or did you tie up a lot of your business logic within the GUI controls themselves? – PaulMcKenzie Oct 07 '22 at 14:27
  • I use mvc, yes. – JenyaKh Oct 07 '22 at 14:34
  • What is it about my answer that you don't get? Did you actually try to code it? – Anders Oct 08 '22 at 00:37
  • @Anders, I am sorry but I really do not understand it. Could you, please, write the pseudo code? – JenyaKh Oct 08 '22 at 04:05

0 Answers0