Attribute Programming with ATL


Now you're going to get into some code and learn firsthand how attributes save you a lot of time and headaches. For the first lesson, you are going to create an ATL project consisting of one ATL object. You will then create a small Microsoft Foundation Classes (MFC) application which uses that object.

To begin, open the New Project dialog by clicking New, Project from the File menu. Select Visual C++ Projects from the list of project types on the left and select the ATL Project template from the list on the right. Give your project the name ATLAttributes and click OK to close the dialog.

You'll notice that the ATL Project Wizard dialog doesn't contain a lot of options compared to some other projects. However, if you click the Application Settings hyperlink on the left, you'll see one option of particular interest. The Attributed option, shown in Figure 15.2, lets the ATL Project Wizard know to create attributed code. If this option is unchecked, the wizard will create code the old way, as was shown in Listing 15.1. For this project obviously, we want the Attributed option to be checked, so click Finish to close the dialog.

Figure 15.2. A single check box in the ATL Project Wizard determines whether the project will use attributed code.


Even though you haven't created any ATL objects, the ATL Project Wizard has already generated attributed code for your project. To see this, open the ATLAttributes.cpp file in the ATLAttributes project, which can be found in the Solution Explorer. You should see code similar to Listing 15.3.

Listing 15.3 Examining the Module Attribute
 1: // ATLAttributes.cpp : Implementation of DLL Exports.  2:  3: #include "stdafx.h"  4: #include "resource.h"  5:  6: // The module attribute causes DllMain, DllRegisterServer and  7: // DllUnregisterServer to be automatically implemented for you  8: [ module(dll, uuid = "{0B9369CB-6C62-4EC0-8A39-0A0807F63ABC}",  9:     name = "ATLAttributes", 10:     helpstring = "ATLAttributes 1.0 Type Library", 11:     resource_name = "IDR_ATLATTRIBUTES") ]; 

This file contains a single attribute, the module attribute, which is configured using a number of parameters. The first parameter specifies that the module is a DLL. The uuid parameter is the universally unique identifier (UUID), which assigns the module an identifier guaranteed to be unique on any machine it runs on. The name parameter, as you may have guessed, is the human-readable name given to the DLL, and the helpstring parameter provides a description of the module. The last parameter, resource_name, is the registry resource contained within a resource script file for the module itself. Unlike previous versions of ATL, this is the only registry script created for attributed ATL projects and does not contain registry information for any objects you may add later.

If you were to create an ATL project without attributes, you would see a drastic difference (see Listing 15.4). The module attribute actually replaces all the module support functions needed by a nonattributed ATL project. In other words, the module attribute is responsible for registering and unregistering the DLL, the DLL entry point function (DllMain), the function responsible for returning class objects (DllGetClassObject), the class used by all DLL project (CAtlDllModuleT), and finally the function used to notify the system that the DLL can be unloaded (DllCanUnloadNow).

Listing 15.4 Module Functions for a Nonattributed ATL Project
 1: // NoAttributes.cpp : Implementation of DLL Exports.  2:  3: #include "stdafx.h"  4: #include "resource.h"  5: #include "NoAttributes.h"  6:  7: class CNoAttributesModule : public CAtlDllModuleT< CNoAttributesModule >  8: {  9: public : 10:    DECLARE_LIBID(LIBID_NoAttributesLib) 11:    DECLARE_REGISTRY_APPID_RESOURCEID(IDR_NOATTRIBUTES, 12:         "{BA47FCEC-2FE5-4EE5-989A-2B2DB511E4BC}") 13: }; 14: 15: CNoAttributesModule _AtlModule; 16: 17: // DLL Entry Point 18: extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, 19:                      DWORD dwReason, 20:                     LPVOID lpReserved) 21: { 22:    hInstance; 23:    return _AtlModule.DllMain(dwReason, lpReserved); 24: } 25: 26: // Used to determine whether the DLL can be unloaded by OLE 27: STDAPI DllCanUnloadNow(void) 28: { 29:    return _AtlModule.DllCanUnloadNow(); 30: } 31: 32: // Returns a class factory to create an object of the requested type 33: STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) 34: { 35:    return _AtlModule.DllGetClassObject(rclsid, riid, ppv); 36: } 37: 38: // DllRegisterServer - Adds entries to the system registry 39: STDAPI DllRegisterServer(void) 40: { 41:    // registers object, typelib and all interfaces in typelib 42:    HRESULT hr = _AtlModule.DllRegisterServer(); 43:    return hr; 44: } 45: 46: // DllUnregisterServer - Removes entries from the system registry 47: STDAPI DllUnregisterServer(void) 48: { 49:    HRESULT hr = _AtlModule.DllUnregisterServer(); 50:    return hr; 51: } 


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 © 2008-2017.
If you may any questions please contact us: