|
Chapter 4 — Sample Applications 41FMTestAppUsing the #import DirectiveThis section describes the steps required to create a COM object using SmartPointers and the #import compiler directive. The #import directive is aMicrosoft-specific compiler directive that creates a Smart Pointer wrapper classfrom a type library; that class can be used to create an instance of the requiredCOM object and to use its services. To use the import directive for an instanceof the Filter Manager interface, you would insert the following code in theStdafx.h file after the “#include ” directive.#include // for AfxOleInit…#include extern CComModule _Module;#include #include #import “FilterManager.tlb” no_namespace named_guidsThe CComModule class implements a COM server. This allows a client toaccess the module’s components. When you open your application, you shouldcall _Module.Init( NULL, AfxGetInstanceHandle()). When you close the app, call_Module.Term(). For an example, please examine the InitInstance() method inFMTestApp.cpp.The #import directive creates two header files that reconstruct the type librarycontents in C++ source code. In this case, the files would be named FilterMan-ager.tlh and FilterManager.tli. The primary header file, FilterManager.tlh,contains a typedef macro that expands to the following format:typedef com_ptr_t< com_IIID > IArgusFMPtrThe C++ template class _com_ptr_t used in the above typedef creates a SmartPointer (in this case, IArgusFMPtr) that can be used to access the interface passedin as the template argument (in this case, IArgusFM).Once the Smart Pointer interface is defined in FilterManager.tlh, we can createan instance of a Smart Pointer in our sample application. Note that the onlyargument to the CreateInstance() method of the Smart Pointer is the class ID ofthe Filter Manager component. Because we specified the named_guids modi-fier, the #import directive created the required CLSID in the header file for us,eliminating the need to call CLSIDFromProgID.m_pIFilterMgr.CreateInstance ( CLSID_ArgusFM ) ;Once an instance of IArgusFMPtr has been created, it can be used to access the
PreviousNext |