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

📄 binding.cpp

📁 网络驱动开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//+---------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 2001.
//
//  File:       B I N D I N G . C P P
//
//  Contents:   Functions to illustrate
//              o How to enumerate binding paths.
//              o How to enumerate binding interfaces.
//              o How to enable/disable bindings.
//
//  Notes:      
//
//  Author:     Alok Sinha    15-May-01
//
//----------------------------------------------------------------------------

#include "bindview.h"

//
// Function:  WriteBindings
//
// Purpose:   Write bindings to specified file.
//
// Arguments:
//    fp  [in]  File handle.
//
// Returns:   None.
//
// Notes:
//

VOID WriteBindings (FILE *fp)
{
    INetCfg              *pnc;
    IEnumNetCfgComponent *pencc;
    INetCfgComponent     *pncc;
    LPWSTR               lpszApp;
    HRESULT              hr;
    UINT                 i;


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

    if ( hr == S_OK ) {

        for (i=CLIENTS_SELECTED; i <= PROTOCOLS_SELECTED; ++i) {

            fwprintf( fp, L"--- Bindings of %s ---\n", lpszNetClass[i] );

            //
            // Get Component Enumerator Interface.
            //

            hr = HrGetComponentEnum( pnc,
                                     pguidNetClass[i],
                                     &pencc );
            if ( hr == S_OK ) {

                hr = HrGetFirstComponent( pencc, &pncc );

                while( hr == S_OK ) {

                    //
                    // Write bindings of the component.
                    //

                    WriteBindingPath( fp,
                                      pncc );
                    ReleaseRef( pncc );

                    fwprintf( fp, L"\n" );

                    hr = HrGetNextComponent( pencc, &pncc );
                }

                fwprintf( fp, L"\n" );

                //
                // S_FALSE merely indicates that there are no more components.
                //

                if ( hr == S_FALSE ) {
                    hr = S_OK;
                }

                ReleaseRef( pencc );
            }
            else {
                ErrMsg( hr,
                        L"Couldn't get the component enumerator interface." );
            }
        }

        HrReleaseINetCfg( pnc, FALSE );
    }
    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." );
        }
    }

    return;
}

//
// Function:  WriteBindingPath
//
// Purpose:   Write binding paths of a component.
//
// Arguments:
//    fp    [in]  File handle.
//    pncc  [in]  Network component.
//
// Returns:   None.
//
// Notes:
//

VOID WriteBindingPath (FILE *fp,
                       INetCfgComponent *pncc)
{
    IEnumNetCfgBindingPath  *pencbp;
    INetCfgBindingPath      *pncbp;
    LPWSTR                  lpszName;
    HRESULT                 hr;

    //
    // Write the first component's name.
    //

    hr = pncc->GetDisplayName( &lpszName );

    if ( hr == S_OK ) {
        fwprintf( fp, L"\n%s", lpszName );
    }
    else {
        ErrMsg( hr,
                L"Unable to get the display name of a component, "
                L" some binding paths will not be written." );

       return;
    }

    //
    // Get binding path enumerator.
    //

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

        hr = HrGetFirstBindingPath( pencbp,
                                    &pncbp );

        while( hr == S_OK ) {

            //
            // Write interfaces of the binding path.
            //

            WriteInterfaces( fp,
                             pncbp );

            ReleaseRef( pncbp );

            hr = HrGetNextBindingPath( pencbp,
                                       &pncbp );
            if ( hr == S_OK ) {
                fwprintf( fp, L"\n%s", lpszName );
            }
        }
  
        ReleaseRef( pencbp );
    }
    else {
        ErrMsg( hr,
                L"Couldn't get the binding path enumerator of %s. "
                L"Its binding paths will not be written.",
                lpszName );
    }

    CoTaskMemFree( lpszName );
    return;
}

//
// Function:  WriteInterfaces
//
// Purpose:   Write bindings to specified file.
//
// Arguments:
//    fp     [in]  File handle.
//    pncbp  [in]  Binding path.
//
// Returns:   None.
//
// Notes:
//

VOID WriteInterfaces (FILE *fp,
                      INetCfgBindingPath *pncbp)
{
    IEnumNetCfgBindingInterface *pencbi;
    INetCfgBindingInterface     *pncbi;
    INetCfgComponent            *pnccLower;
    LPWSTR                      lpszName;
    HRESULT                     hr;

    hr = HrGetBindingInterfaceEnum( pncbp,
                                    &pencbi );

    if ( hr == S_OK ) {

        hr = HrGetFirstBindingInterface( pencbi,
                                         &pncbi );

        //
        // Write lower component of each interface.
        //

        while( hr == S_OK ) {

            hr = pncbi->GetLowerComponent ( &pnccLower );

            if ( hr == S_OK ) {

                hr = pnccLower->GetDisplayName( &lpszName );
                if ( hr == S_OK ) {
                    fwprintf( fp, L"-->%s", lpszName );
                    CoTaskMemFree( lpszName );
                }
            }

            ReleaseRef( pnccLower );
            ReleaseRef( pncbi );

            hr = HrGetNextBindingInterface( pencbi,
                                            &pncbi );
        }

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

    return;
}

//
// Function:  EnumNetBindings
//
// Purpose:   Enumerate components and their bindings.
//
// Arguments:
//    hwndTree        [in]  Tree handle.
//    uiTypeSelected  [in]  Type of network component selected.
//
// Returns:   TRUE on success.
//
// Notes:
//

BOOL EnumNetBindings (HWND hwndTree,
                      UINT uiTypeSelected)
{
    INetCfg              *pnc;
    IEnumNetCfgComponent *pencc;
    INetCfgComponent     *pncc;
    LPWSTR               lpszApp;
    HTREEITEM            hTreeItem;
    HRESULT              hr;


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

    if ( hr == S_OK ) {

        //
        // Get Component Enumerator Interface.
        //

        hr = HrGetComponentEnum( pnc,
                                 pguidNetClass[uiTypeSelected],
                                 &pencc );
        if ( hr == S_OK ) {

            hr = HrGetFirstComponent( pencc, &pncc );

            while( hr == S_OK ) {

                //
                // Add the component's name to the tree.
                //

                hTreeItem = AddToTree( hwndTree,
                                       TVI_ROOT,
                                       pncc );
                if ( hTreeItem ) {

                    //
                    // Enumerate bindings.
                    //

                    ListBindings( pncc,
                                  hwndTree,
                                  hTreeItem );
                }

                ReleaseRef( pncc );

                hr = HrGetNextComponent( pencc, &pncc );
            }

            //
            // S_FALSE merely indicates that there are no more components.
            //

            if ( hr == S_FALSE ) {
                hr = S_OK;
            }

            ReleaseRef( pencc );
        }
        else {
            ErrMsg( hr,
                    L"Couldn't get the component enumerator interface." );
        }

        HrReleaseINetCfg( pnc, FALSE );
    }
    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." );
        }
    }

    return hr == S_OK;
}

//
// Function:  ListBindings
//
// Purpose:   Enumerate bindings of network components.
//
// Arguments:
//    pncc          [in]  Network component.
//    hwndTree      [in]  Tree handle.
//    hTreeItemRoot [in]   Parent item.
//
// Returns:   None.
//
// Notes:
//

VOID ListBindings (INetCfgComponent *pncc,
                   HWND hwndTree,
                   HTREEITEM hTreeItemRoot)
{
    IEnumNetCfgBindingPath      *pencbp;
    INetCfgBindingPath          *pncbp;
    HTREEITEM                   hTreeItem;
    ULONG                       ulIndex;
    HRESULT                     hr;
  
    hr = HrGetBindingPathEnum( pncc,
                               EBP_BELOW,
                               &pencbp );
    if ( hr == S_OK ) {

        hr = HrGetFirstBindingPath( pencbp,
                                    &pncbp );

        ulIndex = 1;

        while( hr == S_OK ) {

            //
            // Add an item for the binding path.
            //

            hTreeItem = AddBindNameToTree( pncbp,
                                           hwndTree,
                                           hTreeItemRoot,
                                           ulIndex );

⌨️ 快捷键说明

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