API- . ( , , , ). , , . (locator objects), , . - , IMoniker. IMoniker ; , , , BindToObject:
interface IMoniker : IPersistStream { HRESULT BindToObject([in] IBindCtx *pbc, [in, unique] IMoniker *pmkToLeft, [in] REFIID riid, [out, iid_is(riid)] void **ppv); // remaining methods deleted for clarity // }
, , . BindToObject : " , ". indToObject , , . , . .
. - , , . API- , . , " " . , " ", , (text-based). , , , . , , , - , .
(display name). IMoniker GetDisplayName, . . , , . MkParseDisplayName - , API- .
MkParseDisplayName :
HRESULT MkParseDisplayName( [in] IBindCtx *pbc, // binding Info - [in, string] const OLECHAR *pwszName, // object name - [out] ULONG *pcchEaten, // progress on error - [out] IMoniker **ppmk); // the resultant moniker -
, . , MkParseDisplayName, ProgID, , . , . , . . IMoniker, , (composite moniker).
, . , (Class Moniker). , . CLSID API- CreateClassMoniker.
HRESULT CreateClassMoniker([in] REFCLSID rclsid, [out] IMoniker **ppmk);
Class Moniker MkParseDisplayName1:
clsid:571F1680-CC83-11d0-8C48-0080C73925BA:
, " lsid" ProgID Class Moniker. kParseDisplayName Class Moniker, Gorilla:
HRESULT GetGorillaClass(IApeClass * &rpgc) { rpgc = 0; // declare the CLSID for Gorilla as a display name // CLSID Gorilla const OLECHAR pwsz[] = OLESTR("clsid:571F1680-CC83-11d0-8C48-0080C73925BA:"); // create a new binding context for parsing // and binding the moniker // // IBindCtx *pbc = 0; HRESULT hr = CreateBindCtx(0, &pbc); if (SUCCEEDED(hr)) { ULONG cchEaten; IMoniker *pmk = 0; // ask to convert the display name to a moniker object // // hr = MkParseDisplayName(pbc, pwsz, &cchEaten, &pmk); if (SUCCEEDED(hr)) { // ask the moniker to find or create the object that it // refers to // , // hr = pmk->BindToObject(pbc, 0, IID_IApeClass, (void**)&rpgc); // we now have a pointer to the desired object, so release // the moniker and the binding context // , // pmk->Release(); } pbc->Release(); } return hr; }
, MkParseDisplayName IMoniker::BindToObject, , . , , (placeholder), API- CreateBindCtx2.
Windows NT 4.0 API- , MkParseDisplayName IMoniker::BindToObject:
HRESULT CoGetObject( [in, string] const OLECHAR *pszName, [in, unique] BIND_OPTS *pBindOptions, [in] REFIID riid, [out, iid_is(riid)] void **ppv);
API- :
// pseudo-code from OLE32.DLL // OLE32.DLL HRESULT CoGetObject(const OLECHAR *pszName, BIND_OPTS *p0pt, REFIID riid, void **ppv) { // prepare for failure // *ppv = 0; // create a bind context // IBindCtx *pbc = 0; HRESULT hr = CreateBindCtx(0, &pbc); if (SUCCEEDED(hr)) { // set bind options if provided // , if (pOpt) hr = pbc->SetBindOptions(pOpt); if (SUCCEEDED(hr)) { // convert the display name into a moniker // ULONG cch; IMoniker *pmk = 0; hr = MkParseDisplayName(pbc, pszName, &cch, &pmk); if (SUCCEEDED(hr)) { // ask the moniker to bind to the named object // hr = pmk->BindToObject(pbc, 0, riid, ppv); pmk->Release(); } } pbc->Release(); } return hr; }
CreateInstance:
HRESULT CreateAGorillaAndEatBanana() { IClassFactory *pcf = 0; // declare the CLSID for Gorilla as a display name // CLSID Gorilla const OLECHAR pwsz[] = OLESTR("clsid:571F1680-CC83-11d0-8C48-0080C73925BA:"); // find the class object via the gorilla's class moniker // gorilla's class moniker HRESULT hr = CoGetObject(pwsz, 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); if (SUCCEEDED(hr)) { // tell the new gorilla to eat a banana // hr = pApe->EatBanana(); pApe->Release(); } pcf->Release(); } return hr; }
3.5 , .
Visual Basic API- CoGetObject GetObject. Visual Basic gorilla :
Sub CreateGorillaAndEatBanana() Dim gc as IApeClass Dim ape as IApe Dim sz as String sz = "clsid:571F1680-CC83-11d0-8C48-0080C73925BA:" ' get the class object for gorillas ' gorilla Set gc = GetObject(sz) ' ask Gorilla class object to create a new gorilla ' Gorilla gorilla Set ape = gc.CreateApe() ' ask gorilla to eat a banana ' gorilla ape.EatBanana End Sub
, Visual Basic IApeClass . , Visual Basic IClassFactory - .
1 MkParseDisplayName , . , . Internet Explorer Microsoft, (URL), ( API- MkParseDisplayNameEx).
2 . , CLSCTX, COSERVERINFO, Class Moniker . Class Moniker , , IClassActivator, .