A Step in the Right Direction: Attributes Simplify ATL Development

   

If you've ever used ATL in Visual C++ 6.0, you know that even an ATL Simple Object has a myriad of different components, such as derived classes, structures, maps, registry scripts, and macros. Even the smallest mistake could take a long time to hunt down and find. Listing 15.1 shows the ATL 3.0 code generated when adding an ATL Simple Object.

Listing 15.1 The ATL 3.0 Header File for an ATL Simple Object
 1: // testobj.h : Declaration of the CTestobj  2:  3: #pragma once  4: #include "resource.h"       // main symbols  5:  6: #include "test.h"  7:  8:  9: // Ctestobj 10: 11: class ATL_NO_VTABLE CTestobj : 12:    public CComObjectRootEx<CComSingleThreadModel>, 13:    public CComCoClass<CTestobj, &CLSID_Testobj>, 14:    public IDispatchImpl<ITestobj, &IID_ITestobj, &LIBID_TestLib, 15:            /*wMajor =*/ 1, /*wMinor =*/ 0> 16: { 17: public: 18:     CTestobj() 19:    { 20:    } 21: 22: DECLARE_REGISTRY_RESOURCEID(IDR_TESTOBJ) 23: 24: 25: BEGIN_COM_MAP(CTestobj) 26:    COM_INTERFACE_ENTRY(ITestobj) 27:    COM_INTERFACE_ENTRY(IDispatch) 28: END_COM_MAP() 29: 30: 31:    DECLARE_PROTECT_FINAL_CONSTRUCT() 32: 33:    HRESULT FinalConstruct() 34:    { 35:        return S_OK; 36:    } 37: 38:    void FinalRelease() 39:    { 40:    } 41: 42: public: 43: 44: }; 45: 46: OBJECT_ENTRY_AUTO(__uuidof(testobj), Ctestobj) 

Within Listing 15.1, you can see that this object derives from CComObjectRootEx <CComSingleThreadModel>, CComCoClass, and IDispatchImpl. It also declares a registry script that is contained within an .rgs file. Also, a COM map lists which interfaces this class implements. If the Use Connection Points option were checked during the creation of the project, the amount of source code would greatly increase. Although this may seem like a lot, don't forget that there is an Interface Definition Language (IDL) file that adds more information you need to keep track of. In other words, the wizard has generated the code for you, but now it's up to you to maintain it.

When using attributes, most of what you see in Listing 15.1 doesn't exist anymore. Furthermore, there is no IDL file and no registry script file. The equivalent header file for an ATL Simple Object is shown in Listing 15.2.

Listing 15.2 An ATL Simple Object Using Attributes
// TestObj.h : Declaration of the CTestObj #pragma once #include "resource.h"       // main symbols // ITestObj [     object,     uuid("F4B52C66-A551-40CB-9640-311428C003F9"),     dual,    helpstring("ITestObj Interface"),     pointer_default(unique) ] __interface ITestObj : IDispatch { }; // CTestObj [     coclass,     threading("apartment"),     vi_progid("ATLAttributes.TestObj"),     progid("ATLAttributes.TestObj.1"),     version(1.0),     uuid("40310D6A-EEEC-4E07-914B-B6B111A6D9E5"),     helpstring("TestObj Class") ] class ATL_NO_VTABLE CTestObj :     public ITestObj { public:     CTestObj()     {     }     DECLARE_PROTECT_FINAL_CONSTRUCT()     HRESULT FinalConstruct()     {         return S_OK;     }     void FinalRelease()     {     } }; 

As you can tell by comparing the two listings, the amount of C++ code generated is much less in Listing 15.2. Much of the information that controls the ATL object has been removed and placed within attributes (this is the information you see within the brackets in Listing 15.2). You will explore what some of these attributes do and how they relate to the old way of doing things later this hour.


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net