An Automation Client Program That Uses the Compiler's #import Directive
Now you can use an entirely new way of writing Automation client programs. Instead of using the Add Class Wizard 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"..\Ex23c\debug\Ex23c.tlb" rename_namespace("ClockDriv") using namespace
ClockDriv;
The compiler then generates (and processes) two files,


_COM_SMARTPTR_TYPEDEF(IEx23c, __uuidof(IDispatch));
The _COM_SMARTPTR_TYPEDEF macro generates the IEx23cPtr 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 IEx23c::RefreshWin ( ) {
return _com_dispatch_method(this, 0x4, DISPATCH_METHOD,
VT_EMPTY, NULL, NULL);
}
inline DATE IEx23c::GetTime ( ) {
DATE _result;
_com_dispatch_propget(this, 0x1, VT_DATE, (void*)&_result);
return _result;
}
inline void IEx23c::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:
IEx23cPtr m_clock;
You then create a clock component object using this statement:
m_clock.CreateInstance(__uuidof(Document));
Now you're ready to use the IEx23cPtr 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.