///////////////////////////////////////////////////////////////////// // client.cpp - exchanges COM strings with in-process component // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2006 // ///////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "../tryATL/tryATL.h" #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << "\n Testing in-proc component built with ATL 8.0" << "\n Sending string and getting another back" << "\n ==============================================\n" << endl; // Initialize COM Library CoInitialize(NULL) ; wstring prefix = L"\n Client:\t"; try { CComQIPtr pItest1; pItest1.CoCreateInstance(CLSID_test); // note: Used as instance, not as smart pointer, e.g.: dot notation if(pItest1) { CComBSTR bstr1(L"Message from CComClient"); wcout << prefix.c_str() << L"send: " << (wchar_t*)bstr1; HRESULT hr; hr = pItest1->putString(bstr1); if(!SUCCEEDED(hr)) throw "call to send(BSTR) failed"; BSTR bstr2; hr = pItest1->getString(&bstr2); if(!SUCCEEDED(hr)) throw "call to Receive(BSTR*) failed"; wcout << prefix.c_str() << "recv: " << (wchar_t*)bstr2 << endl; SysFreeString(bstr2); } else { throw "could not create tryATL instance"; } CComQIPtr pItest2; pItest2.CoCreateInstance(CLSID_test); // // note: Used as instance, not as smart pointer, e.g.: dot notation if(pItest2) { CComBSTR bstr(L"Message from CComClient"); wcout << prefix.c_str() << L"send: " << (wchar_t*)bstr; HRESULT hr; hr = pItest2->putString(bstr); if(!SUCCEEDED(hr)) throw "call to send(BSTR) failed"; bstr.Empty(); // avoids memory leak hr = pItest2->getString(&bstr); if(!SUCCEEDED(hr)) throw "call to Receive(BSTR*) failed"; wcout << prefix.c_str() << "recv: " << (wchar_t*)bstr << endl; } else { throw "could not create tryATL instance"; } } catch(const char* msg) { cout << "\n " << msg << "\n\n"; } // Uninitialize COM Library // Note: If you declare CComQIPtr in the same scope as CoInitialize // and CoUninitialize(), then you must call pIString.Release() before // calling CoUninitialize(). If declared in a nested scope, as here, // the destructor, ~CComQIPtr() will call release for you, when it // leaves the inner scope. CoUninitialize() ; cout << "\n\n"; return 0; }