Binding a Moniker

From the client's perspective, binding couldn't be much easier: call the API function BindMoniker or call IMoniker::BindToObject if you want more control over the bind context. LinkUser centralizes this process in CApp::BindAndShow (LINKUSER.CPP), which binds the moniker to obtain an IDescription interface for the object and then asks that object to render its text description with IDescription::GetText:


void CApp::BindAndShow(IMoniker *pmk, IBindCtx *pbc)
{
HRESULT hr;
IDescription *pIDescription;

if (NULL==pbc)
{
hr=BindMoniker(pmk, 0, IID_IDescription
, (void **)&pIDescription);
}
else
{
hr=pmk->BindToObject(pbc, NULL, IID_IDescription
, (void **)&pIDescription);
}

if (SUCCEEDED(hr))
{
const long cch=512;
TCHAR szText[cch];

hr=pIDescription->GetText(szText, cch);
pIDescription->Release();

[Show appropriate message on success or failure.]
}
else
[Show appropriate error message.]

return;
}

That's it—a client needs to do nothing more than ask the moniker to perform its magic. As we'll see shortly, LinkSource does considerable work through the process, but this encapsulation is exactly what makes monikers such a powerful technology. The moniker is a treasure map, and all a client needs to do is ask the moniker for the treasure.