我有一个要在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来确定关闭顺序。只是要求它在帮助程序处理之前让您的服务器收到关闭通知:
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/