///////////////////////////////////////////////////////////////// // CComClient.cpp - client for component IPersistFile // // uses CComQIPtr and CComBSTR // // // // Platform: Dell Dimension 8100, Windows 2000, SP2 // // Application: Demonstration of ATL 7.0 for CSE775 // // Modified: Jim Fawcett, Syracuse Univ, CST 2-187 // // (315) 443-3948, fawcett@ecs.syr.edu // ///////////////////////////////////////////////////////////////// #include "stdafx.h" #include #include #include "_simple.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << "\n Testing in-proc component implementing IPersistFile" << "\n State is a name and a color " << "\n =====================================================\n" << endl; if(argc < 3) { cout << "\n Please enter a name and color on the command line\n\n"; return 1; } // Initialize COM Library CoInitialize(NULL) ; wstring prefix = L"\n Client: \t"; try { CComQIPtr pIsimpletest; pIsimpletest.CoCreateInstance(__uuidof(Csimpletest)); // note: Used as instance, not as smart pointer, e.g.: dot notation if(pIsimpletest) { CComQIPtr pIPFile; pIsimpletest->QueryInterface(__uuidof(IPersistFile),(void**)&pIPFile); if(pIPFile) { wcout << prefix << L"acquired IPersistFile\n"; pIPFile->Load(L"temp.stg",0); // BSTR oldName; BSTR oldColor; pIsimpletest->GetName(&oldName); pIsimpletest->GetColor(&oldColor); wcout << prefix << L"old name: " << oldName; wcout << prefix << L"old color: " << oldColor; CComBSTR name = argv[1]; wcout << prefix << L"sending: " << (wchar_t*)name; HRESULT hr; hr = pIsimpletest->SetName(name); if(!SUCCEEDED(hr)) throw "call to SetName(BSTR) failed"; CComBSTR color = argv[2]; hr = pIsimpletest->SetColor(color); if(!SUCCEEDED(hr)) throw "call to SetColor(BSTR) failed"; wcout << prefix << L"sending: " << (wchar_t*)color << endl; pIPFile->Save(L"temp.stg",true); } else wcout << prefix << L"query for IPersistFile failed"; } else { throw "could not create Isimpletest 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 ; }