Home > Enterprise >  How does Windows terminates applications without a window or console on shutdown?
How does Windows terminates applications without a window or console on shutdown?

Time:11-10

I am currently writing on an background application. It has no window or console running and is also no service. I am using the /SUBSYSTEM:WINDOWS inside my CMake.

So I know there are the WM_CLOSE and the CTRL_CLOSE_EVENT and I could spawn an invisible window to wait for the WM_CLOSE.

My question now is, what happens to the application if it doesn't do anything of this and windows wants to shut down?

Does it simply kill my application "SIGKILL" style or am I missing something here? I want to know of the shutdown to clean some stuff up, mainly handles

CodePudding user response:

On logoff / shutdown all processes associated with that session will be terminated.
(equivalent to TerminateProcess(), i.e. the windows SIGKILL equivalent)

This is described in Logging off / Shutting down on msdn.


Notifications

There are a few ways you can register a handler to get a notification before your process gets terminated, depeding on the type of your application:

  • Console Applications can use SetConsoleCtrlHandler()
  • GUI Applications can handle the WM_QUERYENDSESSION / WM_ENDSESSION messages
    • Note that there's no guarantee you'll get a WM_CLOSE mesage
    • GUI Applications can NOT use SetConsoleCtrlHandler(), due to the handler not being called on logoff / shutdown:
      SetConsoleCtrlHandler() Remarks Section:

      If a console application loads the gdi32.dll or user32.dll library, the HandlerRoutine function that you specify when you call SetConsoleCtrlHandler does not get called for the CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT events.

  • Services can use RegisterServiceCtrlHandlerEx()
Logoff / Shutdown can't be delayed forever

Note that you can't delay logoff / shutdown forever with those notifications; after /- 5 seconds (non-contractual) Windows will show a list of applications that are preventing shutdown, asking the user if he wants to forcefully close them. (this one)

  • If the user chooses "Shutdown anyway" your process will be terminated instantly
  • If the user chooses "Cancel" the logoff / shutdown gets cancelled and your process gets to live

If the user doesn't choose either option within a few seconds then Windows will auto-pick "Shutdown anyway". (resulting in termination of your process)

Good read about this topic: If one program blocks shutdown, then all programs block shutdown by Raymond Chen


Cleanup you need to do

Note that you don't have to free allocated memory, close handles, etc... before your process gets terminated.

  • On process termination all allocated memory will be automatically freed,
  • all handles will be closed,
  • and all allocated resources will be freed.

See Terminating a Process for more details.

So the only thing you would need to worry about is application-specific state. (e.g. if you need to persist in-memory state to the disk)

I think this quote from Raymond Chen about process shutdown really gets the point across:

The building is being demolished.
Don’t bother sweeping the floor and emptying the trash cans and erasing the whiteboards.
And don’t line up at the exit to the building so everybody can move their in/out magnet to out.
All you’re doing is making the demolition team wait for you to finish these pointless housecleaning tasks. i.e. don't bother freeing allocated memory, closing handles, etc... if you know your process is about to be terminated anyway; all you're doing is delaying shutdown / logoff for no reason.

Good reads about this topic:

  • Related