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

标签 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 下无法运行,以便我可以有一个适用于这两种操作系统的解决方案。

有什么帮助吗?

最佳答案

事情在 Vista 时代发生了变化,不太确定这将如何影响您的代码。最好的办法是不要让 Windows 来决定关机顺序。只需要求它让您的服务器在 helper 进程之前获得关闭通知:

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

关于windows - Windows 关闭时应用程序正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8760509/

相关文章:

windows - Lens K8s 无法连接到 Windows 10 中的终端

.net - 持续集成是否消除了对庞大的 Visual Studio 解决方案的需求?

c# - 如何在 C# 中打开 Windows 7 事务处理文件

windows-7 - 在cmd中,如何获取名称中带有插入符号的文件夹路径

windows-7 - JDK 7 Windows 7 自动安装

windows - 使用批处理脚本进行字符串操作

c++ - 在 C++ 中抑制系统 ("ping") 输出

java - 使用cmd打开包含多个空格的文件时出现问题

android - 在 Mac Pro 上进行 Windows 开发

c# - Windows 7 中的rapi.dll 问题 - C#