title: MFC: Not saving data on reboot / WM_ENDSESSION
body: <p>If I test an MFC app by sending it an <code>WM_ENDSESSION</code> and <code>TRUE</code> that is ending, the framework of MFC calls <code>OnCloseDocument()</code>.  All good! But I find that when Windows update or the MSI Installer wants to reboot after installing something it, the 'OnCloseDocument()' is not called and all data is lost.</p>
  <p>Looking at the MFC code you see:</p>
  <pre><code>// when Windows session ends, close all documents
  void CFrameWnd::OnEndSession(BOOL bEnding)
  {
      if (!bEnding)
          return;
  
      CWinApp* pApp = AfxGetApp();
      if (pApp != NULL &amp;&amp; pApp-&gt;m_pMainWnd == this)
      {
          if (AfxGetThreadState()-&gt;m_lastSentMsg.lParam &amp; ENDSESSION_CLOSEAPP)
          {
              // Restart Manager is restarting the application
              CDataRecoveryHandler *pHandler = pApp-&gt;GetDataRecoveryHandler();
              if (pHandler)
              {
                  pHandler-&gt;SetShutdownByRestartManager(TRUE);
  
                  // Just return here rather than doing more processing.
                  // The final autosave will be handled in the WM_CLOSE handler,
                  // because the Restart Manager allows 30 seconds for processing
                  // that message, and only 5 seconds for processing WM_ENDSESSION.
                  return;
              }
          }
  
          AfxOleSetUserCtrl(TRUE);    // keeps from randomly shutting down
          pApp-&gt;CloseAllDocuments(TRUE);
  
          // allow application to save settings, etc.
          pApp-&gt;ExitInstance();
      }
  }
  </code></pre>
  <p>I presume what must be happening is the <code>ENDSESSION_CLOSEAPP</code> section is being called, which doesn't end up saving anything.</p>
  <p>This is the code in the app constructor for the restart manager:</p>
  <pre><code>  // support Restart Manager
    m_dwRestartManagerSupportFlags=AFX_RESTART_MANAGER_SUPPORT_RESTART | AFX_RESTART_MANAGER_SUPPORT_RECOVERY;
  </code></pre>
  <p>Since I don't need autosave, but even if autosave is 5 minutes it still may not have changes saved.   What is the correct way to handle it in MFC to ensure the <code>OnCloseDocument()</code> is called when WM_ENDSESSION is sent?</p>