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

📄 component.cpp

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

    return dwCount;
}

//
// Function:  BindUnbind
//
// Purpose:   Bind/unbind a network component.
//
// Arguments:
//    lpszInfId  [in]  PnpID of the network component to bind/unbind.
//    hwndTree   [in]  Tree handle.
//    fBind      [in]  if TRUE, bind, otherwise unbind.
//
// Returns:   TRUE on success.
//                       
// Notes:
//

BOOL BindUnbind (LPWSTR lpszInfId,
                 HWND hwndTree,
                 BOOL fBind)
{
    INetCfg                   *pnc;
    INetCfgComponent          *pncc;
    INetCfgComponentBindings  *pnccb;
    INetCfgComponent          *pnccToBindUnbind;
    LPWSTR                    lpszApp;
    HTREEITEM                 hTreeItem;
    TVITEMW                   tvItem;
    HRESULT                   hr;
    BOOL                      fChange;


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

    fChange = FALSE;

    if ( hr == S_OK ) {

        //
        // Get a reference to the network component.
        //

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

            //
            // Get a reference to the component's binding.
            //

            hr = pncc->QueryInterface( IID_INetCfgComponentBindings,
                                         (PVOID *)&pnccb );
            if ( hr == S_OK ) {

                //
                // Start with the root item.
                //

                hTreeItem = TreeView_GetRoot( hwndTree );

                //
                // Bind/unbind the network component with every component
                // that is checked.
                //

                while ( hTreeItem ) {

                    ZeroMemory( &tvItem,
                                sizeof(TVITEMW) );

                    tvItem.hItem = hTreeItem;
                    tvItem.mask = TVIF_PARAM | TVIF_STATE;
                    tvItem.stateMask = TVIS_STATEIMAGEMASK;

                    if ( TreeView_GetItem(hwndTree,
                                          &tvItem) ) {

                        //
                        // Is the network component selected?
                        //

                        if ( (tvItem.state >> 12) == 2 ) {

                            //
                            // Get a reference to the selected component.
                            //

                            hr = pnc->FindComponent( (LPWSTR)tvItem.lParam,
                                                     &pnccToBindUnbind );
                            if ( hr == S_OK ) {

                                if ( fBind ) {

                                    //
                                    // Bind the component to the selected component.
                                    //

                                    hr = pnccb->BindTo( pnccToBindUnbind );

                                    if ( !fChange ) {
                                        fChange = hr == S_OK;
                                    }

                                    if ( hr != S_OK ) {
                                        ErrMsg( hr,
                                                L"%s couldn't be bound to %s.",
                                                     lpszInfId, (LPWSTR)tvItem.lParam );
                                    }
                                }
                                else {
                                    //
                                    // Unbind the component from the selected component.
                                    //

                                    hr = pnccb->UnbindFrom( pnccToBindUnbind );

                                    if ( !fChange ) {
                                        fChange = hr == S_OK;
                                    }

                                    if ( hr != S_OK ) {
                                        ErrMsg( hr,
                                                L"%s couldn't be unbound from %s.",
                                                     lpszInfId, (LPWSTR)tvItem.lParam );
                                    }
                                }

                                ReleaseRef( pnccToBindUnbind );
                            }
                            else {
                                ErrMsg( hr,
                                        L"Couldn't get an interface pointer to %s. "
                                        L"%s will not be bound to it.",
                                        (LPWSTR)tvItem.lParam,
                                        lpszInfId );
                            }
                        }
                    }

                    //
                    // Get the next item.
                    //

                    hTreeItem = TreeView_GetNextSibling( hwndTree,
                                                         hTreeItem );
                }

                ReleaseRef( pnccb );
            }
            else {
                ErrMsg( hr,
                        L"Couldn't get a binding interface of %s.",
                        lpszInfId );
            }

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

        //
        // If one or more network components have been bound/unbound,
        // apply the changes.
        //

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

            fChange = hr == S_OK;
        }

        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." );
        }
    }

    return fChange;
}

//
// Function:  ListInstalledComponents
//
// Purpose:   List installed network components of specific class.
//
// Arguments:
//    hwndTree      [in]  Tree handle in which to list.
//    pguidClass    [in]  Class GUID of the network compoent class.
//
// Returns:   None.
//
// Notes:
//

VOID ListInstalledComponents (HWND hwndTree,
                              const GUID *pguidClass)
{
    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,
                                 pguidClass,
                                 &pencc );
        if ( hr == S_OK ) {

            hr = HrGetFirstComponent( pencc, &pncc );

            while( hr == S_OK ) {

                //
                // Add an item to the tree for the network component.
                //

                hTreeItem = AddToTree( hwndTree,
                                       TVI_ROOT,
                                       pncc );

                ReleaseRef( pncc );

                hr = HrGetNextComponent( pencc, &pncc );
            }

            ReleaseRef( pencc );
        }
        else {
            ErrMsg( hr,
                    L"Failed to get the network component enumerator." );
        }

        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:  UninstallComponent
//
// Purpose:   Uninstall a network component.
//
// Arguments:
//    lpszInfId  [in]  PnpID of the network component to uninstall.
//
// Returns:   S_OK on success, otherwise an error code.
//
// Notes:
//

HRESULT UninstallComponent (LPWSTR lpszInfId)
{
    INetCfg              *pnc;
    INetCfgComponent     *pncc;
    INetCfgClass         *pncClass;
    INetCfgClassSetup    *pncClassSetup;
    LPWSTR               lpszApp;
    GUID                 guidClass;
    OBO_TOKEN            obo;
    HRESULT              hr;

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

    if ( hr == S_OK ) {

        //
        // Get a reference to the network component to uninstall.
        //

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

        if ( hr == S_OK ) {

            //
            // Get the class GUID.
            //

            hr = pncc->GetClassGuid( &guidClass );

            if ( hr == S_OK ) {

                //
                // Get a reference to component's class.
                //

                hr = pnc->QueryNetCfgClass( &guidClass,
                                            IID_INetCfgClass,
                                            (PVOID *)&pncClass );
                if ( hr == S_OK ) {

                    //
                    // Get the setup interface.
                    //

                    hr = pncClass->QueryInterface( IID_INetCfgClassSetup,
                                                   (LPVOID *)&pncClassSetup );

                    if ( hr == S_OK ) {

                        //
                        // Uninstall the component.
                        //

                        ZeroMemory( &obo,
                                    sizeof(OBO_TOKEN) );

                        obo.Type = OBO_USER;

                        hr = pncClassSetup->DeInstall( pncc,
                                                       &obo,
                                                       NULL );
                        if ( (hr == S_OK) || (hr == NETCFG_S_REBOOT) ) {

                            hr = pnc->Apply();

                            if ( (hr != S_OK) && (hr != NETCFG_S_REBOOT) ) {
                                ErrMsg( hr,
                                        L"Couldn't apply the changes after"
                                        L" uninstalling %s.",
                                        lpszInfId );
                            }
                        }
                        else {
                            ErrMsg( hr,
                                    L"Failed to uninstall %s.",
                                    lpszInfId );
                        }

                        ReleaseRef( pncClassSetup );
                    }
                    else {
                        ErrMsg( hr,
                                L"Couldn't get an interface to setup class." );
                    }

                    ReleaseRef( pncClass );
                }
                else {
                    ErrMsg( hr,
                            L"Couldn't get a pointer to class interface "
                            L"of %s.",
                            lpszInfId );
                }
            }
            else {
                ErrMsg( hr,
                        L"Couldn't get the class guid of %s.",
                        lpszInfId );
            }

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

        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." );
        }
    }

    return hr;
}

⌨️ 快捷键说明

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