變更元件的系結
網路設定子系統一律會通知物件有關系結變更,而變更會影響通知物件的網路元件。 子系統會呼叫 notify 物件的 INetCfgComponentNotifyBinding::NotifyBindingPath 方法,並傳遞值來指定變更以及變更相關系結路徑之 INetCfgBindingPath 介面的指標。 如果子系統傳遞NCN_DISABLE以停用通知物件的網路元件與特定網路卡共用的系結路徑,則 notify 物件可以啟動與其他網路卡的系結,如下列程式碼所示。
HRESULT CSample::NotifyBindingPath(DWORD dwChangeFlag,
INetCfgBindingPath* pncbp1)
{
INetCfgComponent *pnccLow;
INetCfgComponentBindings *pncbind;
IEnumNetCfgBindingPath *penumncbp;
INetCfgBindingPath *pncbp2;
IEnumNetCfgBindingInterface *penumncbi;
INetCfgBindingInterface *pncbi;
DWORD dwFlags = EBP_BELOW;
ULONG celt = 1; // Request one enumeration element.
HRESULT hr = S_OK;
// Retrieve bindings for the notify object's component (m_pncc)
hr = m_pncc->QueryInterface(IID_INetCfgComponentBindings,
(LPVOID*)&pncbind);
// Determine if notification is about disabling a binding path.
if (SUCCEEDED(hr) && (NCN_DISABLE & dwChangeFlag)) {
// Retrieve enumerator for binding paths for the component.
hr = pncbind->EnumBindingPaths(dwFlags, &penumncbp);
// Reset the sequence and retrieve a binding path.
hr = penumncbp->Reset();
hr = penumncbp->Next(celt, &pncbp2, NULL);
// Ensure the binding path is different.
do {
if (pncbp1 != pncbp2) break;
hr = penumncbp->Skip(celt); // skip one element
hr = penumncbp->Next(celt, &pncbp2, NULL);
} while (SUCCEEDED(hr));
if (SUCCEEDED(hr)) {
// Retrieve enumerator for interfaces of the binding path.
hr = pncbp2->EnumBindingInterfaces(&penumncbi);
// Retrieve a binding interface for the binding path.
hr = penumncbi->Next(celt, &pncbi, NULL);
// Retrieve the lower network component.
hr = pncbi->GetLowerComponent(&pnccLow);
// If the component is a physical network card and binding
// is currently disabled, enable binding.
DWORD dwcc;
hr = pnccLow->GetCharacteristics(&dwcc);
if (SUCCEEDED(hr) && (dwcc & NCF_PHYSICAL)) {
hr = pncbp2->IsEnabled(); // S_FALSE for disabled
if (hr == S_FALSE) hr = pncbp2->Enable(TRUE);
}
}
else return hr;
}
return hr;
}