Is it possible to detach a running process from its console and hand ownership of that console back to the parent process?
That is, I would like a process started from cmd.exe
to detach itself from the console (i.e. closing the console doesn't kill the process) and keep running in the background, while control of the standard input, output and error streams are handed back to cmd.exe
so that the user can continue running commands.
In essence, I'm trying to roughly approximate the appearance of calling fork
on Linux.
I'm currently doing:
#include <windows.h>
int main() {
FreeConsole();
Sleep(10000);
return 0;
}
This keeps the process alive even if the console window is closed, but control of the standard streams is not handed back to cmd.exe
until the sleep times out and the process terminates.
EDIT: the functionality is part of a cross-platform library and may be called from an arbitrary point in the user's code, so refactoring the application into something more Windows-y, calling external commands, restarting the process (or starting the process from another parent process in a two-step launch sequence), creating a Windows service, etc. are unfortunately not viable solutions.