/*============================================================================ * * File Name: StgMain.cpp * * Description: Simple program that opens creates a doc file, and asks * a Tribble to save some info in it. * * Author: Roger Hughes * * Date: 4/1/98 * * *==============================================================================*/ #include #include #include #include "..\OleCharConv.h" // Shim class that converts between char * and wchar_t * #include "..\tribbleinterface.h" // RH added for the Tribble stuff int main(int argc, char *argv[ ]) { // above - param1 = file name // - param2 = tribble name // - param3 = tribble colour printf("Stgmain - Tribble file writer\n"); if(argc != 4) { printf("usage \"StgMain \n"); } else { HRESULT hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED ); if(FAILED(hr)) return 0; // Do it the easy way by using CoCreateInstance(...) OR // Demonstrate the use of ... MULTI_QI mQI[2] = { // ask for a tribble interface and a PersistFile Interface {&IID_ITribble,0,0}, {&IID_IPersistFile,0,0} }; hr = CoCreateInstanceEx (CLSID_Tribble, // The Class We want NULL, // We are not agregating CLSCTX_SERVER, // Test using a local server NULL, // This is the server info - used to specifiy what machine to run on // This is the interesting bit... // For optimisation CoCreateInstanceEx uses a MULTI_QI structure // to get hold of lots of Interface pointers in one go when QIing // across a wire - by using only 1 round trip. 2, // In this case we only want 2 mQI); /* This function supports the standard return value E_INVALIDARG, as well as the following: S_OK - Indicates success. CO_S_NOTALLINTERFACES - At least one, but not all of the interfaces requested in the pResults array were successfully retrieved. The hr field of each of the MULTI_QI structures in pResults indicates with S_OK or E_NOINTERFACE whether the specific interface was returned. E_NOINTERFACE - None of the interfaces requested in the pResults array were successfully retrieved. */ // With 2 Interfaces this bit of code is ridiculous but it demonstrates a point. if((hr == S_OK)||(hr == CO_S_NOTALLINTERFACES)) { ITribble * pTribble = NULL; IPersistFile * pPersistFile = NULL; if(SUCCEEDED(mQI[0].hr)) pTribble = reinterpret_cast(mQI[0].pItf); if(SUCCEEDED(mQI[1].hr)) pPersistFile = reinterpret_cast(mQI[1].pItf); //=== Use of the Interface =================================== if(pTribble) // If we didn't get the Tribble - give up { // set up some properties on the Tribble pTribble->Eat((const unsigned char *)"Some Food"); pTribble->Sleep(); // These two are the ones stored in the file pTribble->put_Name((const unsigned char *)argv[2]); pTribble->put_Colour((const unsigned char *)argv[3]); pTribble->Release(); if(pPersistFile) { COleCharConv fp(argv[1]); hr = pPersistFile->Save(fp,FALSE); // the tribble saves it's data. if(FAILED(hr)) { printf("Could not get the Tribble to Save itself\n"); } } } else printf("Can't get hold of a Tribble\n"); if(pPersistFile) pPersistFile->Release(); } else // big error { printf("CoCreateInstanceEx --- Failed with hr = %lx\n",hr); } CoUninitialize(); } return 0; }