I was debugging an application and placed calls to message boxes (as break points) in the WinMain. At some point I prevented the "while (GetMessage())" loop from executing, but my application worked fine! To test it further, I completely removed the loop:
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
if (!hPrevInstance)
if (!InitApplication(hInstance)) return 0;
if (!InitInstance(hInstance, nCmdShow)) return 0;
MessageBox(NULL, "About to return", "Debug", MB_OK);
return msg.wParam;
};
but the application still worked (as long as I kept that message box alive) - and not just "worked" - like showing a simple window - it maintained serial communications, data acquisition hardware, file creation, registry access, graphics and so on - it all worked as usual.
I have always been sure that all the messages in the main thread are going through that GetMessage() loop, and indeed, it is easy to verify that by putting a trap inside the loop. But with the entire loop commented out - I do not understand what is going on.
//==================================
I am adding this part for future readers, after the solutions are accepted:
A message box in my code must be nothing but a modal dialog box created by the system, most likely in a very similar way that DialoBox() function works.
According to Win32 Online Help,
"The [DialogBox] function displays the dialog box …, disables the owner window, and finally starts its own message loop to retrieve and dispatch messages for the dialog box.
When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter …"
Now, the call to MessageBox() has NULL for the parent - that is why my main window (created in the call to InitInstance) does not get disabled. So far so good: now we have a thread and a message loop within it. The only ambiguity remains in the phrase
“starts its own message loop to retrieve and dispatch messages for the dialog box” – apparently, not just for the dialog box but for all windows created by the current thread.