Tuesday, December 2, 2008

Object Inheritance


Probably the first thing you noticed about this code is the multiple inheritance. Our COM object has three base classes. These base classes are

template classes which implement the standard COM functionality for our object. Each of these classes defines a specific COM behavior.

CComObjectRootEx< > and CComObjectRoot< > are the root ATL object classes. These classes handle all the reference counting and

management of the COM class. This includes implementation of the three required IUnknown interfaces, QueryInterface(), AddRef(), and

Release(). When our CBeepObj object is created by the server, this base class will keep track of it throughout its lifetime.

The template for CComObjectRootEx specifies the argument CComSingleThreadModel. Single threading means that the COM object won't have

to handle access by multiple threads. During the setup of this object we specified "Apartment threading." Apartment threading uses a windows

message loop to synchronize access to the COM object. This approach is the easiest because it eliminates many threading issues.

CComCoClass< > defines the Class factories that create ATL COM objects. Class factories are special COM classes that are used to create COM

objects. The CComCoClass uses a default type of class factory and allows aggregation.

http://devcentral.iticentral.com/articles/DCOM/intro_DCOM/part2/3.php (3 of 6) [7/9/2001 2:52:53 PM]

DevCentral - Understanding DCOM - Part II

IBeepObj is the interface this server implements. An interface is defined as a C++ struct (recall that structs in C++ act like a class but can

have only public members). If you dig into the automatically generated file BeepServer.h, you'll find that the MIDL has created a definition of

our interface.

interface DECLSPEC_UUID(

"36ECA947-5DC5-11D1-BD6F-204C4F4F5020")

IBeepObj : public IUnknown

{

public:

virtual /* [helpstring] */ HRESULT

STDMETHODCALLTYPE Beep(

/* [in] */ long lDuration) = 0;

};

The DECLSPEC_UUID macro lets the compiler associate a GUID with the interface name. Note that our single method "Beep" is defined as a

pure virtual function. When the CBeepObj is defined, it will have to provide an implementation of that function.

One peculiar thing about this Class definition is that it has the ATL_NO_VTABLE attribute. This macro is an optimization that allows for faster

object initialization.

0 comments: