 <p>Create a <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599.aspx#message_only">message-only window</a>, and handle the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa376890.aspx">WM_QUERYENDSESSION</a> and/or <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa376889.aspx">WM_ENDSESSION</a> messages.</p>