Using the Win32 API in Visual C++, I want to create a program under the Windows subsystem that allocates a console with AllocConsole and writes to it with WriteConsole. However, if the user closes the console, the process should keep running in the background. As it stands, I can’t get that to happen. When X is pressed on the console title bar, the process exits.
Is there any particular way of doing this?
Thanks in advance!
The key is to respond to the Console Control Event that is raised when the user attempts to close the console. You can then call FreeConsole to detach your program from the console, and let the console be destroyed. That should keep your program running.
Additional info:
If the process is terminated when the
HandlerRoutineexits, then my suggestion didn’t work as expected. If that’s the case, then you might have a problem. You can try hooking the SC_CLOSE system message, and do theFreeConsolethere before passing the message on. That might work, although I don’t know what it’ll do if the user presses Ctrl+C or Ctrl+Break.The problem is that the control handler exits the process. It might be that calling
FreeConsolein theHandlerRoutineis too late.