This article only applies to threads that are single-threaded apartment model. Threads in the MTA (Multi-Threading Apartment) using the free- threading model introduced in Windows NT version 4.0 and Distributed COM for Windows 95 are not required to dispatch messages. For additional information on free threading, please see the following article in the Microsoft Knowledge Base:
32-bit OLE calls between processes are made using RPC. In Apartment model threading and in single threading, OLE synchronizes the received RPC call with the recipient thread by posting the thread a window message. Consequently a server OLE thread doesn't receive calls from clients if it doesn't dispatch messages. Similarly a client OLE thread doesn't receive notifications from servers if it doesn't dispatch messages.
For example, the following thread blocks can encounter the problems discussed earlier:
CoCreateInstance(CLSID_Hello, NULL, CLSCTX_SERVER,
IID_IUnknown, (void **)&punk);
punk->QueryInterface(IID_IHello, (void **)&phello);
BOOL WaitWithMessageLoop(HANDLE hEvent)
dwRet = MsgWaitForMultipleObjects( 1, // One event to wait for
&hEvent, // The array of events
FALSE, // Wait for 1 event
INFINITE, // Timeout value
QS_ALLINPUT); // Any message wakes up
if(dwRet == WAIT_OBJECT_0)
// The event was signaled, return
} else if(dwRet == WAIT_OBJECT_0 + 1)
// There is a window message available. Dispatch it.
// Something else happened. Return.
CoInitialize(); // or OleInitialize()
// force msg-q to be created just in case, NOP otherwise
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
// msg to myself to do work
PostThreadMessage(GetCurrentThreadId(), WM_USER+1, 0, 0);
while (GetMessage(&msg, NULL, 0, 0))
// this was my message -- time to do my work
if (msg.hwnd == NULL && msg.message == WM_USER+1)
// do my work here, CCI, work, work, release, etc
// if this thread is doing long process, you need to break
that into smaller chunks,
// and post another user message to process further, that
way you don't block the
// messages which need to be processed.
// when done,
Article ID: 136885 - Last Review: Mar 24, 2009 - Revision: 1