3
votes

OK so I have my main window procedure (simplified):

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            if(!loadFiles())
                SendMessage(hwnd, WM_CLOSE, 0, 0);
        }
        break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

loadFiles() is a function to read some text files. If the files are missing, it posts an error message and then returns false. I handle this by sending the WM_CLOSE message, which sends WM_DESTROY? The program calls PostQuitMessage(0) and.. nothing. The process is still sitting in background, now using 100% CPU on core 1. Here is my message loop :

MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return Msg.wParam;

Very simple, I've looked around and seen people using all kinds of different loops, most with PeekMessage() function.. its worth noting I have several child windows containing different controls, I show and hide them when the user selects different tabs, but I don't think it matters because PostQuitMessage(0) is supposed to post WM_QUIT to the message queue, which destroys the child windows aswell right? Any help? Thanks

1
There is nothing wrong with the code you have shown so it has to be a problem with code you have not shown yet. Use a debugger to find out where your code is getting stuck.Remy Lebeau
It is pointless to put return 0; under your WM_DESTROY and WM_CASE case, because you have already declared break under them, which will eventually return 0 after breaking. I don't see anything wrong with your code. Maybe it has something to do with your other codes?CLearner
Try sending WM_CLOSE using PostMessage() instead of SendMessage()?Edward Clements

1 Answers

3
votes

According to MSDN WM_CREATE is sent during CreateWindow. So you are destroying the window right during creation. I am not sure that this is supposed to work. Also, why so complicated? The documentation says

If an application processes this message, it should return zero to continue creation of the window. If the application returns –1, the window is destroyed and the CreateWindowEx or CreateWindow function returns a NULL handle.

So just return -1 in case loadFiles() fails and handle CreateWindow returning NULL accordingly.