/////////////////////////////////////////////////////////////////////// // ConsoleClient.cpp - client for component MarshalInterfacePtr // // uses CComQIPtr and CComBSTR // // // // Platform: Dell Dimension 8100, Windows 2000, SP2 // // Application: Demonstration of Marshaling Interface Pointers // // CSE775 - Distributed Objects, Spring 2004 // // Modified: Jim Fawcett, Syracuse Univ, CST 2-187 // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////////////// #include #include #include #include #include "..\\MarshalInterfacePtr\\MarshalInterfacePtr.h" #include "..\\threads\\thrdsWithProc.h" using namespace std; class proc : public processing { public: proc(IStream* pStream) : _pStream(pStream) {} void run() { // create second STA CoInitialize(NULL); // unmarshal interface pointer IMarshalIFPtr* pIMarsh = 0; CoGetInterfaceAndReleaseStream(_pStream,IID_IMarshalIFPtr,(LPVOID*)&pIMarsh); // and use it CComBSTR bstr; HRESULT hr = pIMarsh->getString(&bstr); if(!SUCCEEDED(hr)) throw "call to getString(BSTR*) failed"; wcout << "\n In child thread (second STA): " << (wchar_t*)bstr << endl; CoUninitialize(); } private: IStream* _pStream; }; // int main(int argc, char* argv[]) { cout << "\n Testing Marshaling of Interface Pointers " << "\n ==========================================\n" << endl; // create first STA CoInitialize(NULL) ; try { CComQIPtr pIMarsh; pIMarsh.CoCreateInstance(CLSID_MarshalIFPtr); // note: Used as instance, not as smart pointer, e.g.: dot notation if(pIMarsh) { // demonstrate component CComBSTR bstr = "hi there"; HRESULT hr; hr = pIMarsh->putString(bstr); if(!SUCCEEDED(hr)) throw "call to putString(BSTR) failed"; hr = pIMarsh->getString(&bstr); if(!SUCCEEDED(hr)) throw "call to getString(BSTR*) failed"; wcout << "\n In main thread (Primary STA): " << (wchar_t*)bstr << endl; // marshal interface pointer to stream IStream* pStream = 0; CoMarshalInterThreadInterfaceInStream(IID_IMarshalIFPtr,pIMarsh,&pStream); // create child thread that will receive marshaled interface pointer proc tproc(pStream); thread t(tproc); t.start(); t.wait(); } else { throw "could not create IMarshalInterfacePtr 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 ; }