Re: COM Apartments - Message Loops
"Ron Ayoub" <ronaldayoub@yahoo.com> wrote in message
news:1161859463.726678.67990@i42g2000cwa.googlegroups.com
So an STA thread that creates a COM object (or host one in other
words) has a hidden window with an associated message queue.
First, a message queue is associated with a thread, not with a
particular window, hidden or otherwise. Second, the hidden window is
created as soon as CoInitialize is called, whether or not the thread
later creates a COM object.
That is,
it is a UI thread. Furthermore, it needs to pump messages explicitly.
That is something not done behind the scenes. So it needs to have a
wait-get-peek-dispatch kind of loop. Is this correct?
Yes.
So is it the
case that DispatchMessage() understand the COM environment and will
automatically make the cross apartment methods calls for you or do you
need to write code to also do that.
I don't understand this statement at all. All DispatchMessage does is
take a message and call a corresponding window proc. It doesn't know nor
care about COM.
When a thread has an interface pointer to an object from a different STA
thread, what it really has is a pointer to a proxy (assuming you follow
COM rules and properly marshal your interface pointers). A thread calls
a method on this proxy. The proxy packs all parameters into a single
array of bytes (a marshalling packet) and posts a custom message to the
hidden window in the target STA thread. Eventually, the STA thread picks
up the message in its message pump, and dispatches it to the COM-created
hidden window. The window proc of that window uses the message
parameters to retrieve the marshalling packet and a reference to the
stub, and passes the packet to the stub. The stub unpacks parameters,
builds the appropriate stack frame and finally calls the actual object.
Any results and [out] parameters travel back to the caller in a similar
fashion.
--
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