I'm having a hard time to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages correctly. I'm always ready to shutdown so I always return TRUE to WM_QUERYENDSESSION. In WM_ENDSESSION I perform some cleanup and then post the WM_QUIT message. The problem with my code is that it will shutdown on WM_ENDSESSION and my process is cleanly terminated but for some reason my application interrupts the windows shutdown sequence and the system will not shutdown. The system will shutdown smoothly on the second attempt (when my process is already dead). What am I doing wrong? The code: Code: int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow) { ... // Main message loop while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg) || !IsDialogMessage(msg.hwnd,&msg) ) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_COMMAND: if (LOWORD(wParam) == MTM_EXIT) { CloseApp(hwndDlg,wParam,lParam); PostQuitMessage(0); return TRUE; } break; case WM_QUERYENDSESSION: return TRUE; case WM_CLOSE: case WM_ENDSESSION: CloseApp(hwndDlg,wParam,lParam); PostQuitMessage(0); return TRUE; ... } }
I found a solution and posted it into another forum: http://www.codeguru.com/forum/showthread.php?t=442396