Re: C++ COM STA model
George <George@discussions.microsoft.com> wrote:
My understanding is, inside the STA, no message queue is needed, and
the message queue is only needed when there is call from other
apartment, right?
Well, yes and no. It is true that _COM_ only needs you to run a message
pump if you want to accept incoming calls.
However, there are cases where some Windows applications broadcast
window messages to all top-level windows (see SendMessage with
HWND_BROADCAST option). In particular, Windows Shell (aka Explorer) does
that to initiate a DDE conversation when you double-click certain files
(e.g. .doc and other MS Office files). Now, your thread happens to have
a top-level window (the hidden one created by COM): if you don't
dispatch messages, Explorer will hang (for some 30 seconds, if I recall
correctly) waiting for its broadcast to be processed by this window.
The bottom line is, an STA thread should run a message pump whether or
not it expects incoming COM calls. Otherwise some seemingly unrelated
applications - most importantly the shell - may freeze.
For call from other apartment, mentioned in the tutorials, I think
the call from other apartment are translated into messages into the
message queue, and the thread in the STA is responsible for retrieve
the message, invoke the object, and finally return the method call
result by the means of message into the queue again, right?
You are just responsible for retrieving the message with GetMessage, and
delivering it to its target window with DispatchMessage - just as you do
with any other window message. The window procedure of that hidden COM
window will take care of invoking the right method on the right object.
That's the whole point of that window's existence.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925