///////////////////////////////////////////////////////////////////// // client.cpp - demonstrates ATL_OutProcST // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2007 // ///////////////////////////////////////////////////////////////////// /* * Module Operations * ================= * - Created as an empty Win32 Console Application, this module * demonstrates what a client needs to do to activate and use * an Out-of-Process COM object. * - We also demonstrate how to send and receive SafeArrays of BSTRs. * * Required Files: * =============== * client.cpp, ATL_OutProcST.h * * Build Process: * ============== * - Build and register ATL_OutProcST COM server * (registration is part of the build process with ATL) * - Build and run client * * Maintenance History * =================== * 25 Feb 2007 * - first release */ #define _WIN32_WINNT 0x0501 #include #include #include "..\ATL_OutProcST.h" #include void main() { std::wcout << L"\n Demonstrating use of ATL_OutProcST "; std::wcout << L"\n ====================================\n"; HRESULT hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED); if(!SUCCEEDED(hr)) { std::wcout << L"\n could not initialize COM"; } try { // COM wrapper objects should be embedded inside a scope // between calls to CoInitialize(Ex) and CoUninitialize // so that their destructors get called BEFORE the COM // libraries are shut down with CoUninitialize. // Doing this inside a try-catch block is a good way to // do that, because the wrappers will throw exceptions if // calls fail. // CComQIPtr pArrayOfStrings; pArrayOfStrings.CoCreateInstance(CLSID_StringArrays); if(pArrayOfStrings != 0) { CComBSTR bstr1(L"First string"); CComBSTR bstr2(L"Second string"); CComBSTR bstr3(L"Third string"); ///////////////////////////////////////////////////////////////// // Here's how you define an array to control the dimensions // CComSafeArrayBound bound[1]; // one-dimensional array // bound[0].SetCount(3); // three elements // bound[0].SetLowerBound(0); // index from 0 // CComSafeArray arrayOfStrings(bound,3); CComSafeArray arrayOfStrings(3); arrayOfStrings[0] = bstr1; arrayOfStrings[1] = bstr2; arrayOfStrings[2] = bstr3; for(int i=0; i<(int)arrayOfStrings.GetCount(); ++i) std::wcout << L"\n client sending: " << arrayOfStrings[i].Copy(); std::wcout << L"\n"; pArrayOfStrings->PutStrings(arrayOfStrings); SAFEARRAY returnArray; SAFEARRAY* pReturnArray = &returnArray; CComSafeArrayBound bound[1]; CComSafeArray SafeReturn; pArrayOfStrings->GetStrings(&pReturnArray); SafeReturn.Attach(pReturnArray); for(int i=0; i<(int)SafeReturn.GetCount(); ++i) std::wcout << L"\n client received: " << SafeReturn[i].Copy(); } else { throw std::exception("CoCreateInstance failed"); } // Don't call release with pStrings. CComQIPtr destructor takes // care of that. } catch(std::exception& ex) { std::wcout << L"\n " << ex.what() << L"\n\n"; return; } std::wcout << L"\n\n"; CoUninitialize(); }