I have an application that is started by a service. The application has no User Interface, it contains a DataModule and a TMyApplication implementation that calls Forms.Application.HandleMessage in a loop (like SvcMgr.TServiceApplication). I hooked the MainWindow to log any messages the hidden window of Forms.Application receives.
- If I manually start the application so it runs on my useraccount, I receive WM_QUERYENDSESSION and WM_ENDSESSION messages on the hidden window of Forms.Application when logging off/shutting down.
- If I start my application using the service, the application runs under the SYSTEM account. When running under the system account I receive only one message,
$0000001A
(WM_WININICHANGE?), at systemshutdown before my application is forcefully terminated by Windows. So no WM_QUERYENDSESSION and WM_ENDSESSION messages.
How can I detect a Windows shutdown on a SYSTEM account and close my application gracefully?