Returning console ownership to parent process
<p>Is it possible to detach a running process from its console <em>and</em> hand ownership of that console back to the parent process?</p>

<p>That is, I would like a process started from <code>cmd.exe</code> 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 <code>cmd.exe</code> so that the user can continue running commands.</p>

<p>In essence, I'm trying to roughly approximate the appearance of calling <code>fork</code> on Linux.</p>

<p>I'm currently doing:</p>

<pre><code>#include &lt;windows.h&gt;

int main() {
    FreeConsole();
    Sleep(10000);
    return 0;
}
</code></pre>

<p>This keeps the process alive even if the console window is closed, but control of the standard streams is not handed back to <code>cmd.exe</code> until the sleep times out and the process terminates.</p>

<p>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.</p>