How does one react to "Exit from Windows"?

I like my applications to close down in an orderly way no matter how
they are terminated.

There is no problem if the main window is closed from within the
application or terminated with ALT-F4 or by "End task" in the task list
or any WM_QUIT message. However, if the user exits from Windows while
the application is running I have a problem.

WM_QUERYENDSESSION and WM_ENDSESSION are not detected in my
Application.OnMessage handler. Also code after the Application.Run is
not executed nor is code in an ExitProc procedure. These are executed if
the application is responding to a WM_QUIT message.

I would be very grateful for any suggestions.

The specific application in which I encountered the problem is one that
runs minimized and times other processes that are run during the Windows
session. If the Windows session ends these processes must also
terminate, and I want to record this in a text log file. Could there be
a problem with a text buffer not being flushed? I use Windows for
Workgroups with Norton Desktop 3.1.