⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binding.cpp

📁 网络驱动开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:

            if ( hTreeItem ) {

                //
                // Enumerate interfaces.
                //

                ListInterfaces( pncbp,
                                hwndTree,
                                hTreeItem );
            }

            ReleaseRef( pncbp );

            hr = HrGetNextBindingPath( pencbp,
                                       &pncbp );

            ulIndex++;
        }
  
        ReleaseRef( pencbp );
    }
    else {
       LPWSTR  lpszName;

       if ( pncc->GetDisplayName(&lpszName) == S_OK ) {

          ErrMsg( hr,
                  L"Couldn't get the binding path enumerator of %s. "
                  L"Its binding paths will not be shown.",
                  lpszName );

          CoTaskMemFree( lpszName );
       }
       else {
          ErrMsg( hr,
                  L"Couldn't get the binding path enumerator of a "
                  L"network component. The binding paths will not "
                  L"be shown." );
       }
    }

    return;
}

//
// Function:  ListInterfaces
//
// Purpose:   Enumerate interfaces of a binding path.
//
// Arguments:
//    pncbp         [in]  Binding path.
//    hwndTree      [in]  Tree handle.
//    hTreeItemRoot [in]  Parent item.
//
// Returns:   None.
//
// Notes:
//

VOID ListInterfaces (INetCfgBindingPath *pncbp,
                     HWND hwndTree,
                     HTREEITEM hTreeItemRoot)
{
    IEnumNetCfgBindingInterface *pencbi;
    INetCfgBindingInterface     *pncbi;
    INetCfgComponent            *pnccBound;
    HTREEITEM                   hTreeItem;
    HRESULT                     hr;

    hr = HrGetBindingInterfaceEnum( pncbp,
                                    &pencbi );

    if ( hr == S_OK ) {

        hr = HrGetFirstBindingInterface( pencbi,
                                         &pncbi );
        hTreeItem = hTreeItemRoot;

        while( (hr == S_OK) && hTreeItem ) {

            //
            // Add lower component of every interface to the tree.
            //

            hr = pncbi->GetLowerComponent( &pnccBound );

            hTreeItem = AddToTree( hwndTree,
                                   hTreeItem,
                                   pnccBound );

            ReleaseRef( pnccBound );
            ReleaseRef( pncbi );

            hr = HrGetNextBindingInterface( pencbi,
                                            &pncbi );
        }

        //
        // If hr is S_OK then, the loop terminated due to error in adding
        // the binding path to the tree and pncbi has a reference to an
        // interface.
        //

        if ( hr == S_OK ) {

            ReleaseRef( pncbi );
        }

        ReleaseRef( pencbi );
    }
    else {
        ErrMsg( hr,
                L"Couldn't get the binding interface enumerator."
                L"The binding interfaces will not be shown." );
    }

    return;
}

//
// Function:  HandleBindingPathOperation
//
// Purpose:   
//
// Arguments:
//    hwndOwner    [in]  Owner window.
//    ulSelection  [in]  Option selected.
//    hItem        [in]  Item selected.
//    lParam       [in]  lParam of the item.
//
// Returns:   None.
//
// Notes:
//

VOID HandleBindingPathOperation (HWND hwndOwner,
                                 ULONG ulSelection,
                                 HTREEITEM hItem,
                                 LPARAM lParam)
{
    switch( ulSelection ) {

        case IDI_ENABLE:
        case IDI_DISABLE:

            //
            // Enable/disable binding path.
            //

            EnableBindingPath( hwndOwner,
                               hItem,
                               (LPWSTR)lParam,
                               ulSelection == IDI_ENABLE );
    }

    return;
}

//
// Function:  EnableBindingPath
//
// Purpose:   Enable/disable binding path.
//
// Arguments:
//    hwndOwner      [in]  Owner window.
//    hItem          [in]  Item handle of the binding path.
//    lpszPathToken  [in]  Path token of the binding path.
//    fEnable        [in]  if TRUE, enable, otherwise disable.
//
// Returns:   None.
//
// Notes:
//

VOID EnableBindingPath (HWND hwndOwner,
                        HTREEITEM hItem,
                        LPWSTR lpszPathToken,
                        BOOL fEnable)
{
    INetCfg              *pnc;
    INetCfgBindingPath   *pncbp;
    LPWSTR               lpszInfId;
    LPWSTR               lpszApp;
    HRESULT              hr;

    //
    // Get PnpID of the owner component.
    //

    lpszInfId = GetComponentId( hwndOwner,
                                hItem );

    if ( lpszInfId ) {

        hr = HrGetINetCfg( TRUE,
                           APP_NAME,
                           &pnc,
                           &lpszApp );

        if ( hr == S_OK ) {

            //
            // Find the binding path reference.
            //

            pncbp = FindBindingPath( pnc,
                                     lpszInfId,
                                     lpszPathToken );

            if ( pncbp ) {

                //
                // Enable/disable.
                //

                hr = pncbp->Enable( fEnable );

                if ( hr == S_OK ) {
                    hr = pnc->Apply();

                    if ( hr == S_OK ) {

                        //
                        // Refreshe the state of the item representing the
                        // binding path.
                        //

                        RefreshItemState( hwndOwner,
                                          hItem,
                                          fEnable );
                    }
                    else {
                        ErrMsg( hr,
                                L"Failed to apply changes to the binding path." );
                    }
                }
                else {
                    if ( fEnable ) {
                        ErrMsg( hr,
                                L"Failed to enable the binding path." );
                    }
                    else {
                        ErrMsg( hr,
                                L"Failed to disable the binding path." );
                    }
                }

                ReleaseRef( pncbp );
            }

            HrReleaseINetCfg( pnc,
                             TRUE );
        }
        else {
            if ( (hr == NETCFG_E_NO_WRITE_LOCK) && lpszApp ) {
               ErrMsg( hr,
                       L"%s currently holds the lock, try later.",
                       lpszApp );

               CoTaskMemFree( lpszApp );
            }
            else {
               ErrMsg( hr,
                       L"Couldn't get the notify object interface." );
            }
        }
    }
    else {
          ErrMsg( HRESULT_FROM_WIN32(GetLastError()),
                  L"Couldn't determine the owner of the binding path." );
    }

    return;
}

//
// Function:  GetComponentId
//
// Purpose:   Find the PnpID of a network component.
//
// Arguments:
//    hwndTree  [in]  Tree handle.
//    hItem     [in]  Item handle of the binding path.
//
// Returns:   PnpID of the network component.
//
// Notes:
//

LPWSTR GetComponentId (HWND hwndTree,
                       HTREEITEM hItem)
{
    LPWSTR       lpszInfId;
    HTREEITEM    hTreeItemParent;
    TVITEMW      tvItem;

    lpszInfId = NULL;

    //
    // Get the item handle of the owner component.
    //

    hTreeItemParent = TreeView_GetParent( hwndTree,
                                          hItem );
    if ( hTreeItemParent ) {

        //
        // Get lParam of the owner component. lParam is the PnpID.
        //

        ZeroMemory( &tvItem,
                    sizeof(TVITEMW) );

        tvItem.hItem = hTreeItemParent;
        tvItem.mask = TVIF_PARAM;

        if ( TreeView_GetItem(hwndTree,
                              &tvItem) ) {

            lpszInfId = (LPWSTR)tvItem.lParam;
        }
    }

    return lpszInfId;
}

//
// Function:  WriteBindings
//
// Purpose:   Find the binding path with a give path token.
//
// Arguments:
//    pnc                    [in]  INetCfg reference.
//    lpszInfId              [in]  PnpID of the network component.
//    lpszPathTokenSelected  [in]  Path token of the binding path to search.
//
// Returns:   Reference to the binding path on success, otherwise NULL.
//
// Notes:
//

INetCfgBindingPath *FindBindingPath (INetCfg *pnc,
                                     LPWSTR lpszInfId,
                                     LPWSTR lpszPathTokenSelected)
{
    INetCfgComponent       *pncc;
    IEnumNetCfgBindingPath *pencbp;
    INetCfgBindingPath     *pncbp;
    LPWSTR                 lpszPathToken;
    HRESULT                hr;
    BOOL                   fFound;


    fFound = FALSE;

    //
    // Get the component reference.
    //

    hr = pnc->FindComponent( lpszInfId,
                             &pncc );

    if ( hr == S_OK ) {
     
        hr = HrGetBindingPathEnum( pncc,
                                   EBP_BELOW,
                                   &pencbp );
        if ( hr == S_OK ) {

            hr = HrGetFirstBindingPath( pencbp,
                                        &pncbp );

            // Enumerate each binding path and find the one
            // whose path token matches the specified one.
            //

            while ( !fFound && (hr == S_OK) ) {

                hr = pncbp->GetPathToken( &lpszPathToken );

                if ( hr == S_OK ) {
                    fFound = !wcscmp( lpszPathToken,
                                       lpszPathTokenSelected );

                    CoTaskMemFree( lpszPathToken );
                }

                if ( !fFound ) {
                    ReleaseRef( pncbp );

                    hr = HrGetNextBindingPath( pencbp,
                                               &pncbp );
                }
            }

            ReleaseRef( pencbp );
        }
        else {
            ErrMsg( hr,
                    L"Couldn't get the binding path enumerator interface." );
        }
    }
    else {
        ErrMsg( hr,
                L"Couldn't get an interface pointer to %s.",
                lpszInfId );
    }

    return (fFound) ? pncbp : NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -