(instantiation) , . , Chimp:
HRESULT CreateChimp(/* [out] */ IApe * &rpApe) { extern const CLSID CLSID_Chimp; rpApe = 0; IClassFactory *pcf = 0; HRESULT hr = CoGetClassObject(CLSID_Chimp, CLSCTX_ALL, 0, IID_IClassFactory, (void**)&pcf); if (SUCCEEDED(hr)) { hr = pcf->CreateInstance(0, IID_IApe, (void**)&rpApe); pcf->Release(); } return hr; }
: Chimp. , (suboperations) CoGetClassObject, CreateInstance, Release. , . , , , . IPC/RPC (Interprocess Communication/Remote Procedure Call), . , , , CreateInstance . IClassFactory, , , . API- : CoCreateInstanceEx, , CoGetClassObject IClassFactory::CreateInstance .
CoCreateInstanceEx CLSID , . teInstanceEx , . CoCreateInstanceEx , . CoCreateInstanceEx, . , , , CoGetClassObject. CoCreateInstanceEx , CoGetClassObject. , CoCreateInstanceEx IClassFactory::CreateInstance, QueryInterface, . , .
CoGetClassObject, CoCreateInstanceEx CLSCTX COSERVERINFO. , CoCreateInstanceEx . MULTI_QI, QueryInterface, :
typedef struct tagMULTI_QI { // which interface is desired? // ? const IID *piid; // null on input, will contain the pointer on output // , [iid_is(piid)] IUnknown *pItf; // will contain the HRESULT from QueryInterface on output // HRESULT QueryInterface HRESULT hr; } MULTI_QI;
, QueryInterface, . QueryInterface , . , , CoCreateInstanceEx, . .
MULTI_QI CoCreateInstanceEx :
HRESULT CoCreateInstanceEx( [in] REFCLSID rclsid, // what kind of object? - ? [in] IUnknown *pUnkOuter,// for aggregation - [in] DWORD dwClsCtx, // locality? - ? [in] COSERVERINFO *pcsi, // host/security info - / [in] ULONG cmqi, // how many interfaces? - ? [out, size_is (cmqi)] MULTI_QI *prgmq // where to put itfs );
, CoCreateInstanceEx S_OK. ( ) , CoCreateInstanceEx CO_S_NOTALLINTERFACES, . HRESULT MULTI_QI, , , . CoCreateInstanceEx , CoCreateInstanceEx HRESULT SEVERITY_ERROR, , .
CoCreateInstanceEx , . :
[object,uuid(753A8F7C-A7FF-11d0-8C30-0080C73925BA)] interface IEgghead : IUnknown { import "unknwn.idl"; HRESULT ContemplateNavel(void); }
, :
void CreateChimpEatBananaAndThinkAboutIt(void) { // declare and initialize an array of MULTI_QI's // MULTI_QI MULTI_QI rgmqi[2] = { { &IID_IApe, 0, 0 }, { &IID_IEgghead, 0, 0 } }; HRESULT hr = CoCreateInstanceEx( CLSID_Chimp, // make a new chimp - 0, // no aggregation - CLSCTX_ALL, // any locality - 0, // no explicit host/security info // 2, // asking for two interfaces - 2 rgmqi); // array of MULTI_QI structs - MULTI_QI if (SUCCEEDED(hr)) { // hr may be CO_S_NOTALLINTERFACES, so check each result // hresult CO_S_NOTALLINTERFACES, // if (hr == S_OK || SUCCEEDED(rgmqi[0].hr)) { // it is safe to blindly cast the resultant ptr to the type // that corresponds to the IID used to request the interface // // , IID, // I * = reinterpret_cast<IApe*>(rgmqi[0].pItf); assert(pApe); HRESULT hr2 = pApe->EatBanana(); assert(SUCCEEDED(hr2)); pApe->Release(); } if(hr == S_OK || SUCCEEDED(rgmqi[1].hr)) { IEgghead *peh = reinterpret_cast<IEgghead*>(rgmqi[1].pItf); assert(peh); HRESULT hr2 = peh->ContemplateNavel(); assert(SUCCEEDED(hr2)); peh->Release(); } } }
3.3 , CoCreateInstanceEx . , . , CoCreateInstanceEx.
ateInstance , :
HRESULT CreateChimpAndEatBanana(void) { // declare and Initialize a MULTI_QI // MULTI_QI MULTI_QI mqi = { &IID_IApe, 0, 0 }; HRESULT hr = CoCreateInstanceEx( CLSID_Chimp, // make a new chimp - , // aggregation - CLSCTX_ALL, // any locality - , // no explicit host/security Info // / 1, // asking for one interface - &mqi); // array of MULTI_QI structs - MULTI_QI if (SUCCEEDED(hr)) { IApe *pApe = reinterpret_cast<IApe*>(mqi.pItf); assert(pApe); // use the new object - hr = pApe->EatBanana(); // release the Interface pointer // pApe->Release(); } return hr; }
COSERVERINFO, CoCreateInstanceEx, CoCreateInstance1:
HRESULT CoCreateInstance( [in] REFCLSID rclsid, // what kind of object? - ? [in] IUnknown *pUnkOuter, // for aggregation - [in] DWORD dwClsCtx, // locality? - ? [in] REFIID riid, // what kind of interface - [out, iid_is(riid)] void **ppv); // where to put itf
, CoCreateInstance
HRESULT CreateChimpAndEatBanana(void) { I * = 0; HRESULT hr = CoCreateInstance( CLSID_Chimp, // make a new chimp , // aggregation - CLSCTX_ALL, // any locality IID_IApe, // what kind of itf - (void**)&pApe); // where to put iff if (SUCCEEDED(hr)) { assert(pApe); // use the new object hr = pApe->EatBanana(); // release the interface pointer // pApe->Release(); } return hr; }
. , CoCreateInstance CoCreateInstanceEx:
// pseudo-code for implementation of CoCreateInstance API // API- CoCreateInstance HRESULT CoCreateInstance(REFCLSID rclsid, IUnknown *pUnkOuter, DWORD dwCtsCtx, REFIID riid, void **ppv) { MULTI_QI rgmqi[] = { &riid, 0, 0 }; HRESULT hr = CoCreateInstanceEx(rclsid, pUnkOuter, dwClsCtx, 0, 1, rgmqi); *ppv = rgmqi[0].pItf; return hr; }
CoCreateInstance, COSERVERINFO . CoCreateInstance CLSCTX_REMOTE_SERVER SCM CLSID - , .
3.4 , CoCreateInstanceEx CoGetClassObject IClassFactory::CreateInstance. , CoCreateInstanceEx CoGetClassObject. , CoCreateInstanceEx , - , , CoGetClassObject. , , , IClassFactory::CreateInstance . IClassFactory::CreateInstance SCM, , CoCreateInstanceEx. , CoCreateInstanceEx, IPC RPC - .
1 CoCreateInstance . CoCreateInstanceEx Windows NT 4.0, , API- . CoGetClassObject , NT 4.0 COSERVERINFO. , CoCreateInstance , CoCreateInstanceEx. , CoGetClassObject, MULTI_QI , CoGetClassObjectEx . IMoniker::BindToObject MULTI_QI.