tags:

views:

471

answers:

1

Im trying to write hello world in win32 but when i close the main window, the app continues to run

My window procedure:

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
    case WM_DESTROY:
     PostQuitMessage(0);
     return 0;
    }
    return DefWindowProc(hWnd, msg, wparam, lparam);
}

Event loop:

while ((bret = GetMessage(&msg, hWndMain, 0, 0) != 0)
{
 if (bret == -1)
 {
  DWORD error = GetLastError();
  return 1;
 }
 TranslateMessage(&msg);
 DispatchMessage(&msg);
}

Now, then i get WM_DESTROY by clicking on the top-right hand corner x, Instead of GetMessage() returning 0 to signify getting WM_QUIT, it instead returns -1 and GetLastError() whinges about error 1400, which is "invalid window handle" ...I'm perplexed.

+7  A: 

Normally you would pass NULL and not a window handle to GetMessage(), this would explain why you are getting ERROR_INVALID_WINDOW_HANDLE since after WM_DESTROY and friends complete, the window will no longer exist. The WM_QUIT posted by PostQuitMessage is a thread message, so GetMessage with a handle will never pick it up.

Anders