Page #41 ( )

IApeClass , , I . , I . , IApe, . , , (generically). IOleItemContainer:

 // from oleidl.idl   oleidl.idl [ object, uuid(0000011c-0000-0000-C000-000000000046) ] interface IOleItemContainer : IOleContainer {       // ask for object named by pszItem        //    ,   pszItem    HRESULT Get0bject( [in] LPOLESTR pszItem,                           // which object?    ?                      [in] DWORD dwSpeedNeeded,                           // deadline                      [in, unique] IBindCtx *pbc,                            // binding info                           [in] REFIID riid,                            // which interface?    ?                      [out, iid_is(riid)] void **ppv);                           // put it here!      !       // remaining methods deleted for clarity        //          } 

, GetObject . IOleItemContainer. Gorilla "Ursus":

 HRESULT FindUrsus(IApe * &rpApe)  {       // bind a reference to the  class object        //              rpApe = 0;     IOleItemContainer *poic = 0;     HRESULT hr = CoGetClassObject(CLSID_Gorilla, CLSCTX_ALL, 0,                     IID_IOleItemContainer, (void**)&poic);     if (SUCCEEDED(hr)) {           // ask Gorilla class object for Ursus           //       Gorilla     Ursus         hr = poic->GetObject(OLESTR("Ursus"),                                 BINDSPEED_INDEFINITE, 0,                                  IID_IApe, (void**)&rpApe);         poic->Release();     }     return hr; } 

, IOleItemContainer (Item Moniker), .

. IClassFactory:

 // from unknwn.idl   unknwn.idl [ object, uuid(00000001-0000-0000-C000-000000000046) ] interface IClassFactory : IUnknown {   HRESULT CreateInstance( [in] IUnknown *pUnkOuter,                            [in] REFIID riid,                            [out, iid_is(riid)] void **ppv) ;   HRESULT LockServer([in] BOOL bLock); } 

IClassFactory, . IClassFactory, , , . , Microsoft Transaction Server (MTS), IClassFactory ( MTS).

IClassFactory : LockServer CreateInstance. LockServer 6. CreateInstance . IApeClass::CreateApe, , , , CreateInstance. CreateInstance 4. , , . CreateInstance .

, IClassFactory IApeClass, IClassFactory::CreateInstance :

 HRESULT CreateAGorillaAndEatBanana()  {     IClassFactory *pcf = 0;       // find the class object            HRESULT hr = CoGetClassObject(CLSID_Gorilla, CLSCTX_ALL, 0,                                    IID_IClassFactory, (void **)&pcf);     if (SUCCEEDED(hr)) {         IApe *pApe = 0;           // use the  class object to create a gorilla           //            gorilla          hr = pcf->CreateInstance(0, IID_IApe, (void**)&pApe);           // we're done with the class object, so release it            //          ,              pcf->Release();         if (SUCCEEDED(hr)) {             // tell the new gorilla to eat a banana              //                        hr = pApe->EatBanana();             pApe->Release();         }     }     return hr; } 

, IApeClass IClassFactory.

, Gorilla IClassFactory:

 class GorillaClass : public IClassFactory {   public:     IMPLEMENT_UNKNOWN_NO_DELETE(GorillaClass)      BEGIN_INTERFACE_TABLE(GorillaClass)        IMPLEMENTS_INTERFACE(IClassFactory)      END_INTERFACE_TABLE()      STDMETHODIMP CreateInstance(IUnknown *pUnkOuter,                                 REFIID riid, void **ppv) {         *ppv = 0;         if (pUnkOuter != 0)               // we don't support aggregation yet               //                       return CLASS_E_NOAGGREGATION;               // create a new instance of our C++ class Gorilla               //          ++-  Gorilla         Gorilla *p = new Gorilla;         if (p == 0)             return E_OUTOFMEMORY:               // increment reference count by one               //                  p->AddRef();               // store the resultant interface pointer into *ppv               //           *ppv         HRESULT hr = p->QueryInterface(riid, ppv);               // decrement reference count by one, which will delete the               // object if QI fails               //          ,               //           QI         p->Release();               // return result of Gorilla::QueryInterface               //       Gorilla::QueryInterface         return hr;     }      STDMETHODIMP LockServer(BOOL bLock); }; 

LockServer . , CreateInstance, , C++ Gorilla , . , QueryInterface AddRef, Release. QueryInterface, . " " (bracketing) QueryInterface AddRef/Release. QueryInterface, Release , . QueryInterface , Release . , Release, .



Suschnost' tehnologii SOM
Essential COM
ISBN: 0201634465
EAN: 2147483647
Year: N/A
Pages: 103
Authors: Don Box

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net