tags:

views:

157

answers:

2

If you create a new ATL project and add a simple COM object to it (note: an object and not a control) that uses the Apartment threading model, will there be a message pump running under the hood? I want to create a hidden window that is a member of my COM object class but I'm not sure if any messages will actually be delivered to it or not. Is this handled behind the scenes or does it matter what sort of application is actually creating the COM object?

+2  A: 

No, an ATL COM object does not implement a message pump by default. Your code must explicitly use on via a normal Windowing library or explicit message pump implementation.

JaredPar
Thanks. I think implementing one would be difficult without knowing more about ATL (and COM) internals.
Rob
@Rob, you don't need to understand ATL or COM internals here. A standard Win32 message pump will do just nicely.
JaredPar
But where would the code for this message pump reside? DLLMain?
Rob
@Rob, it depends on how your code is created. The thread which creates your STA object is required to create a message pump in order for that object to properly function
JaredPar
A: 

COM uses a message pump under the hood for communicating with your COM object when necessary, if your COM object lives in an appartment. That's how methods are safely called on your object (by being serialized by the message queue) when called by an object in another appartment (STA or the MTA).

You can't get at the message pump - COM puts it together for you only when its needed. You'll notice when debugging that you call methods on your object directly - you're not jumping through a message pump. You would be, of course, if you were putting together multiple objects that live in different appartments.

If you need a window, you can create one using standard methods. ATL provides simple windows classes such as CWindow and CWindowImpl which can make this easier.

iantr