Search code examples
windowswinapiconsole-application

Is there a way for a console application to fully release the console it was launched from?


I have a Windows console application written in C/C++. I want, eventually (depending on X condition), to completely release the console my App was launched from and stop the console from waiting for my App to finish (so the user can keep using the console as usual). Kind of, if X happens then return the console control to the user and keep running in the background. Is it possible?

I tried calling FreeConsole but it seems like the console keeps waiting for my program to finish. Maybe I'm missing something like closing std I/O handlers or another step.

I appreciate any help you can provide.

Best!


Solution

  • Kind of, if X happens then return the console control to the user and keep running in the background. Is it possible?

    Not that I'm aware of, no. Once a new console process begins running, it is attached to the console that launched it and remains attached until it exits. There is no API (that I know of) to break that attachment for a console process.

    You will likely have to make your console app spawn a new non-console process for its remaining background work, and then exit from the original process.

    Or, you could do all of your work in a background service app to begin with, and have your console app simply communicate with it via an IPC mechanism when needed.