添加组件

当子系统添加网络组件时,网络配置子系统可以通知通知对象。 初始化 notify 对象后,子系统调用 notify 对象的 INetCfgComponentNotifyGlobal::GetSupportedNotifications 方法,以检索对象所需的通知类型。 如果 notify 对象指定在添加网络组件时需要通知,则子系统将调用 notify 对象的 INetCfgComponentNotifyGlobal::SysNotifyComponent 方法,并传递NCN_ADD以通知通知对象子系统安装了网络组件。 如果拥有 notify 对象的组件应绑定到指定的组件,则 notify 对象应执行操作以方便绑定。 例如,下面的代码演示了如果指定的组件是所需的物理网络卡,则通知对象如何将其组件绑定到指定的组件。

HRESULT CSample::SysNotifyComponent(DWORD dwChangeFlag,
        INetCfgComponent* pnccItem)
{
    HRESULT hr = S_OK;
    INetCfgComponentBindings *pncfgcompbind;
    // Retrieve bindings for the notify object's component (m_pncc)
    hr = m_pncc->QueryInterface(IID_INetCfgComponentBindings, 
                                (LPVOID*)&pncfgcompbind);
    // Determine if notification is about adding a component
    if (SUCCEEDED(hr) && (NCN_ADD & dwChangeFlag)) {
        // Retrieve the characteristics of the added component
        DWORD dwcc;
        hr = pnccItem->GetCharacteristics(&dwcc);
        // Determine if the added component is a physical adapter
        if (SUCCEEDED(hr) && (dwcc & NCF_PHYSICAL)) {
            // Determine the component's ID
            LPWSTR pszwInfId;
            hr = pnccItem->GetId(&pszwInfId);
            if (SUCCEEDED(hr)) {
                // Compare the component's ID to the required ID
                // and if they are the same perform the binding.
                static const TCHAR c_szCompId[] = TEXT("BINDTO_NIC");
                if (!_tcsicmp(pszwInfId, c_szCompId)) {
                    hr = pncfgcompbind->BindTo(pnccItem);
                }
            }
        }
    }
    return hr;
}