0

Trying to capture when the user clicks end task from the task manager, and when the user logs off of windows, in order to perform clean-up tasks before closing the application.

After reading this answer&comments and this answer I got the following conclusions:

  1. if my application is a GUI application, I should handle the message WM_CLOSE and WM_ENDSESSION in my message loop, which will cover both cases (end task and log off).
  2. If my application is a console application, I should use SetConsoleCtrlHandler to handle CTRL_CLOSE_EVENT and CTRL_LOGOFF_EVENT, which will handle both cases.

Are my conclusions correct?

If my app doesn't have any window nor a message loop nor a console attached (It's technically a background process that is compiled with the -mwindows flag in MinGW gcc in order to hide the console window):

  • How to capture end task and log off in this case?
NadAlaba
  • 292
  • 3
  • 15
  • What *problem* are you trying to solve? Also, there is no such thing as *"a GUI app"*. There are threads that have a message queue, and there are threads that do not. Applications usually own a mixture of both threads, and a thread that doesn't have a message queue can be propagated to one that does at any time. – IInspectable Jun 02 '22 at 11:00
  • @IInspectable I'm trying to perform clean-up tasks before my application gets closed by log-off or end task, so I need to capture these 2 events. What do you mean there is no such thing as a GUI app? What do you call an application that has been compiled with `-mwindows` flag on [MinGW gcc?](https://gcc.gnu.org/onlinedocs/gcc/x86-Windows-Options.html) – NadAlaba Jun 02 '22 at 11:13
  • 1
    The `-mwindows` flag is a linker option that instructs the linker to produce an image that targets the WINDOWS subsystem (see [/SUBSYSTEM](https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem) for reference). The only difference between a program targeting the CONSOLE subsystem and a program targeting the WINDOWS subsystem is, that the system will allocate a console for the former. There is no other difference. Specifically, a CONSOLE application can display a GUI, and a WINDOWS application can allocate or attach to a console. – IInspectable Jun 02 '22 at 12:42
  • @IInspectable Oh! Didn't know that, thnx. Does that mean I can't use `SetConsoleCtrlHandler` because my app doesn't have a console attached? Also, I can't capture `WM_ENDSESSION` unless I put a message loop. Right? How about `signal(SIGINT, handlerFunction)`, will it capture end task and log off without the need for a console or a message loop? – NadAlaba Jun 02 '22 at 13:30
  • 1
    @NadAlaba https://stackoverflow.com/a/18866072/65863: "*By the way, even though it is called a "Console Control Handler," [`SetConsoleCtrlHandler()`] works perfectly fine in applications that use `WinMain (...)` instead of `main (...)`.*" Also, `signal()` doesn't exist on Windows. – Remy Lebeau Jun 02 '22 at 18:52

0 Answers0