1

I am making a console app, and I want my program,to do some cleaning up, after you close its console I researched a lot but I haven't found any good answer to this

Misiu
  • 41
  • 5
  • well, what have you found in your search? – Neil Butterworth Jan 31 '23 at 17:14
  • Wait for a keypress at the end of main, then do the cleanup. e.g. `std::cout << "Press enter to stop program...\n"; char c; std::cin >> c;` – Pepijn Kramer Jan 31 '23 at 17:14
  • @Pep not really - suppose it is closed via the x button, or by other means? – Neil Butterworth Jan 31 '23 at 17:15
  • @NeilButterworth Ok that's another thing :) And my guess it is platform dependent then. And I would only know how to do that for windows apps not console apps. – Pepijn Kramer Jan 31 '23 at 17:16
  • Build a service that can do work after the console app closes. – crashmstr Jan 31 '23 at 17:17
  • Please be more specific. What exactly do you want to clean up? Is this a "console app" as in an application that has its own console, or one that runs in a console/terminal? – molbdnilo Jan 31 '23 at 17:36
  • it runs in a windows console, and I want to make some things ater the user closes the console – Misiu Jan 31 '23 at 17:39
  • `SetConsoleCtrlHandler()` is exactly what this is for... but it only gives you a limited time before your process is terminated anyway. Make sure to do the most important cleanup stuff immediately, and let other things fail. Except for the handler, when the user closes the console on Windows, that is unfortunately the same as a kill process command. – Dúthomhas Jan 31 '23 at 18:08
  • oh thank you, this is exacly what I needed – Misiu Jan 31 '23 at 18:14

1 Answers1

1

There are many ways for a process to be terminated—closing the console window is just one. In general, you cannot depend on being able to clean up on close. Alternatives are to clean-as-you-go and/or clean-up leftovers on the next launch.

If you're worried only about the closing the console situation. Here are a couple options on Windows. Unfortunately, neither of them is trivial to implement.

  • Rather than creating a console app, create a "Windows" app that doesn't have a GUI and instead creates its own console. When the user closes that console, the process will still be running. I cannot remember if there's a way to attach your "Windows" program to the console it was started from. If there were, I think Visual Studio would have used that rather than the devenv.com and devenv.exe trick.

  • Create a console app that launches a second (non-console) program. The second process is your main program, but it to direct its output to the console app (e.g., using a named pipe). Likewise, the console app would have to direct user input to the second process. If the user closes the console (or the console app), the second process can continue to run.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Also look into [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). Used judiciously and everything cleans up after itself automatically except in the event of a fatal error. And in the event of a fatal error could you trust the system to be in a good enough state to clean up correctly? – user4581301 Jan 31 '23 at 18:29