Where s the IDL File?


Where's the IDL File?

One aspect of attributed programming that is drastically different from using ATL 3.0 is the fact that there is no IDL file within the project. The IDL file is used to define various items within your project, such as library parameters, interfaces in the project and their associated coclasses, interface methods, and custom data types, such as enumerations and structures. This information would then be compiled using the Microsoft Interface Definition File (MIDL) compiler to create extra header files and the project's type library. However, if you look through Solution Explorer for the project you just created, you won't find an IDL file. So where did it go?

Find and open the file AttributeTest.h in the ATLAttributes project using the Solution Explorer. You'll see that the various wizards you have used so far have placed some attributes at the top of the file, as shown in Listing 15.5.

Listing 15.5 Viewing Attributes of the ATL Simple Object Header File
 1: // AttributeTest.h : Declaration of the CAttributeTest  2:  3: #pragma once  4: #include "resource.h"       // main symbols  5:  6: // IAttributeTest  7: [  8:    object,  9:    uuid("59FE1059-227C-4566-A4D2-6CD1086AD857"), 10:    dual,    helpstring("IAttributeTest Interface"), 11:    pointer_default(unique) 12: ] 13: __interface IAttributeTest : IDispatch 14: { 15:    [id(1), helpstring("method HelloWorld")] 16:             HRESULT HelloWorld(  CHAR* Buffer); 17: }; 18: 19: // CAttributeTest 20: [ 21:    coclass, 22:    threading("apartment"), 23:    vi_progid("ATLAttributes.AttributeTest"), 24:    progid("ATLAttributes.AttributeTest.1"), 25:    version(1.0), 26:    uuid("8025D44A-49D1-495D-BD92-FA352BDA806B"), 27:    helpstring("AttributeTest Class") 28: ] 29: 30: class ATL_NO_VTABLE CAttributeTest : 31:    public IAttributeTest 32: { 33: public: 34:    CAttributeTest() 35:    { 36:    } 37:    DECLARE_PROTECT_FINAL_CONSTRUCT() 38: 39:    HRESULT FinalConstruct() 40:    { 41:        return S_OK; 42:    } 43: 44:    void FinalRelease() 45:    { 46:    } 47: 48: public: 49:    STDMETHOD(HelloWorld)(CHAR* Buffer); 50: }; 

If you've seen IDL code before, the attributes at the beginning of the header file should look pretty familiar. Rather than keeping all the information needed for the MIDL compiler in one file, Microsoft decided to keep that information together with the code that implements it. One advantage of this is that all the necessary pieces are all in one place, and you don't have to waste time looking through different files if you need to change or add something.

The attributes in Listing 15.5 replace IDL and serves to define the interface contained within that file. The first block of attributes within the brackets is the IAttributeTest interface you added earlier. Following that is the actual declaration of the interface and its method(s). Using this methodology helps you to better read and understand your code rather than navigating back and forth between different files trying to piece everything together.

The last step left in the ATL project before you can implement your MFC client is to implement your HelloWorld function. Open the AttributeTest.cpp file using the Solution Explorer and locate the HelloWorld function. This function will simply check to see whether the parameter that was passed is a valid block of memory and, if so, copy a string into it and return. This function is shown in Listing 15.6.

Listing 15.6 Implementing the Interface Method HelloWorld
1: STDMETHODIMP CAttributeTest::HelloWorld(CHAR* Buffer) 2: { 3:    if( Buffer == NULL ) 4:        return E_POINTER; 5: 6:    _tcscpy( Buffer, _T(" Hello from ATL with attributes" )); 7: 8:    return S_OK; 9: } 


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