An Automation Client Program Using the Compiler's #import Directive

Now there's an entirely new way of writing Automation client programs. Instead of using ClassWizard to generate a class derived from COleDispatchDriver, you use the compiler to generate header and implementation files directly from a component's type library. For the clock component, your client program contains the following statement:

#import"..\ex25c\debug\ex25c.tlb" rename_namespace("ClockDriv") using namespace ClockDriv;

The compiler then generates (and processes) two files, ex25c.tlh and ex25c.tli, in the project's Debug or Release subdirectory. The TLH file contains the IEx25c clock driver class declaration plus this smart pointer declaration:

_COM_SMARTPTR_TYPEDEF(IEx25c, __uuidof(IDispatch));

The _COM_SMARTPTR_TYPEDEF macro generates the IEx25cPtr pointer type, which encapsulates the component's IDispatch pointer. The TLI file contains inline implementations of member functions, some of which are shown in the following code:

inline HRESULT IEx25c::RefreshWin ( ) {
    return _com_dispatch_method(this, 0x4, DISPATCH_METHOD, 
        VT_EMPTY, NULL, NULL);
}

inline DATE IEx25c::GetTime ( ) {
    DATE _result;
    _com_dispatch_propget(this, 0x1, VT_DATE, (void*)&_result);
    return _result;
}

inline void IEx25c::PutTime ( DATE _val ) {
    _com_dispatch_propput(this, 0x1, VT_DATE, _val);
}

Note the similarity between these functions and the COleDispatchDriver member functions you've already seen. The functions _com_dispatch_method, _com_dispatch_propget, and _com_dispatch_propput are in the runtime library.

In your Automation client program, you declare an embedded smart pointer member in your view class (or in another class) like this:

IEx25cPtr  m_clock;

Then you create a clock component object with this statement:

m_clock.CreateInstance(__uuidof(Document));

Now you're ready to use the IEx25cPtr class's overloaded -> operator to call the member functions defined in the TLI file:

m_clock->PutTime(COleDateTime::GetCurrentTime());
m_clock->RefreshWin();

When the m_clock smart pointer object goes out of scope, its destructor calls the COM Release function.

The #import directive is the future of COM programming. With each new version of Visual C++, you'll see COM features moving into the compiler, along with the document_view architecture itself.