1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem with windows shutdown

Discussion in 'MFC' started by Metabaron, Dec 29, 2007.

  1. Metabaron

    Metabaron New Member

    Joined:
    Dec 29, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
    ...
    }
    }
     
    Last edited by a moderator: Dec 30, 2007
  2. Metabaron

    Metabaron New Member

    Joined:
    Dec 29, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0

Share This Page