Windows关闭时正常关闭应用程序

时间:2012-01-06 15:51:46

标签: windows windows-7

我有一个应用程序,我想在Windows关闭(或用户注销)时正常关闭。这曾经工作过(在xp中),但在去年的某个时候它没有人注意到。在Windows 7下它也破坏了(但不同)。

我们的产品有一个主进程(server.exe),可启动许多其他进程。正常关闭会让server.exe询问它开始关闭的所有进程。但是,当我调试此代码时,似乎其他进程已经终止。我们的主进程(server.exe)是处理WM_QUERYENDSESSION和WM_ENDSESSION消息的唯一进程。下面的代码(这曾经在XP下工作但不再有):

LRESULT CALLBACK master_wnd_proc
(
   HWND hwnd,      /* (in) handle to window */
   UINT uMsg,      /* (in) message identifier */
   WPARAM wParam,  /* (in) first message parameter */
   LPARAM lParam   /* (in) second message parameter */
)
{
   LRESULT result;   /* return value */
   long msg_code;

   switch (uMsg)
   {
      case WM_ENDSESSION:
         if (wParam)
         {
            msg_code = PCS_WINDOWS_SHUTDOWN;
            if( lParam & 0x01L )
               msg_code = WINDOWS_SHUT_CLOSE;
            if( lParam & 0x40000000L )
               msg_code = WINDOWS_SHUT_CRIT;
            if( (unsigned long)lParam & 0x80000000 )
               msg_code = WINDOWS_SHUT_LOGOFF;
            MsgGenerate(msg_code, MSG_SEVERE, MSG_LOG, "");

            ipc_declare_shutdown( msg_code );

            //We need one more message in the message queue
            //to force the message loop, below, to exit.
            PostQuitMessage(EXIT_SUCCESS);

            /* WARNING:  Don't call MsgGenerate() after this point! */
         }
         result = 0;
         break;

      case WM_QUERYENDSESSION:

         /* return TRUE to say "okay to shutdown"
          * If FALSE is returned, then other processes are not stopped
          * and the session isn't ended.
          */
         result = TRUE;
         break;

      /* for a Windows TIMER or for an IPC prompt, handle
       * the old server code and tcall messages and
       * once-per-second work.  Notice that the
       * once-per-second work could just be done on the WM_TIMER
       * and the tcall work could just be done on the WM_APP_IPC_POSTED
       * but I've merged them together here.  The merge isn't
       * necessary to fix a bug or anything, but rather to
       * make the code more robust in the face of unexpected
       * conditions.
       */
      case WM_TIMER:
      case WM_APP_IPC_POSTED:
         /* now handle tcall messages */
         (void) server();

         result = FALSE;
         break;

      default:
         result = DefWindowProc (hwnd, uMsg, wParam, lParam);
         break;
   }

   return result;
}

似乎我们在去年改变了一些东西,需要所有子进程来处理WM_QUERYENDSESSION消息(我真的想避免这种情况)。我似乎无法找到任何关于进程何时收到或未收到此消息的信息。

我已经使用新的API在Windows 7下工作,但是想知道为什么它在XP下崩溃所以我可以找到一个适用于这两种操作系统的解决方案。

任何帮助?

1 个答案:

答案 0 :(得分:3)

在Vista时代发生了变化,不太确定这会对你的代码产生什么影响。最好的办法是不要将它留给Windows来确定关机顺序。只需要让它让您的服务器在帮助程序进程之前获得关闭通知:

   DWORD dwLevel, dwFlags;
   BOOL fOkay = GetProcessShutdownParameters(&dwLevel, &dwFlags);
   ASSERT(fOkay);
   if (fOkay && dwLevel > 0x100) {
       fOkay = SetProcessShutdownParameters(dwLevel + 1, SHUTDOWN_NORETRY);
       ASSERT(fOkay);
   }