///////////////////////////////////////////////////////////////// // client.cpp - Uses CComQIPtr and CComBST wrapper classes // // to Simplify Client Code. // // // // Note: Client type code is used here and in OuterComp as // // well, since OuterComp is a client of InnerComp // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2007 // ///////////////////////////////////////////////////////////////// /* +--------+ IOuterComponent +----------------+ | client |------------------->| OuterComponent | | |---------+ +-------+--------+ +--------+ | | | | | +-------V--------+ +--------->| InnerComponent | IInnerComponent +----------------+ - OuterComponent is created and used by client - InnerComponent is created by OuterComponent and used by both */ #include "stdafx.h" #include #include #include #include "..\OuterComp\OuterComp.h" #include "..\InnerComp\InnerComp.h" using namespace std; template string title(const string& t) { string underline(t.length()+2,ch); return (string("\n ") + t + string("\n ") + underline); } // //----< client entry point >----------------------------------------- int _tmain(int argc, _TCHAR* argv[]) { cout << title<'='>("Client Calling a Component that Calls Another"); CoInitialize(NULL); { // Create instance of OuterComponent and use it. // OuterComponent will create instance of InnerComponent and use it. CComQIPtr pOuter; HRESULT hr = pOuter.CoCreateInstance(CLSID_OuterComponent); if(pOuter != 0) { CComBSTR str(OLESTR("\n Hi There from client!")); pOuter->SendStringForModification(&str); // Here, client will see modifications made by both Outer and Inner // components. wcout << str.Detach() << L"\n\n"; // Client gets pointer to inner component from the OuterComponent // and uses it. IInnerComponent* pInner; hr = pOuter->ReturnInnerPtr(&pInner); if(SUCCEEDED(hr)) { CComBSTR str(L"\n modified by client"); pInner->ModifyStringAgain(&str); wcout << str.Detach() << L"\n\n"; } pInner->Release(); } } CoUninitialize(); return 0; }