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

📄 component.cpp

📁 网络驱动开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//+---------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 2001.
//
//  File:       C O M P O N E N T . C P P
//
//  Contents:   Functions to illustrate
//              o How to enumerate network components.
//              o How to install protocols, clients and services.
//              o How to uninstall protocols, clients and services.
//              o How to bind/unbind network components.
//
//  Notes:      
//
//  Author:     Alok Sinha    15-May-01
//
//----------------------------------------------------------------------------

#include "bindview.h"

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

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

        case IDI_BIND_TO:
        case IDI_UNBIND_FROM:

            //
            // Bind/unbind components.
            //

            BindUnbindComponents( hwndOwner,
                                  hItem,
                                  (LPWSTR)lParam,
                                  ulSelection == IDI_BIND_TO );
    }

    return;
}

//
// Function:  BindUnbindComponents
//
// Purpose:   Bind/unbind a network component.
//
// Arguments:
//    hwndOwner  [in]  Owner window.
//    hItem      [in]  Item handle of the network component.
//    lpszInfId  [in]  PnpID of the network component.
//    fBindTo    [in]  if TRUE, bind, otherwise unbind.
//
// Returns:   None.
//
// Notes:
//

VOID BindUnbindComponents( HWND hwndOwner,
                           HTREEITEM hItem,
                           LPWSTR lpszInfId,
                           BOOL fBindTo)
{
    BIND_UNBIND_INFO  BindUnbind;

    BindUnbind.lpszInfId = lpszInfId;
    BindUnbind.fBindTo = fBindTo;

    DialogBoxParam( hInstance,
                    MAKEINTRESOURCE(IDD_BIND_UNBIND),
                    hwndOwner,
                    BindComponentDlg,
                    (LPARAM)&BindUnbind ); 

    return;
}

//
// Function:  InstallComponent
//
// Purpose:   Install a network component.
//
// Arguments:
//    hwndDlg     [in]  Owner window.
//    pguidClass  [in]  Class GUID of type of network component to install.
//
// Returns:   S_OK on success, otherwise and error code.
//
// Notes:
//

HRESULT InstallComponent (HWND hwndDlg,
                          const GUID *pguidClass)
{
    INetCfg              *pnc;
    INetCfgClass         *pncClass;
    INetCfgClassSetup    *pncClassSetup;
    LPWSTR               lpszApp;
    OBO_TOKEN            obo;
    HRESULT              hr;

    //
    // Get INetCfg reference.
    //

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

    if ( hr == S_OK ) {

        //
        // Get network component's class reference.
        //

        hr = pnc->QueryNetCfgClass( pguidClass,
                                    IID_INetCfgClass,
                                    (PVOID *)&pncClass );

        if ( hr == S_OK ) {

            //
            // Get Setup class reference.
            //

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

            if ( hr == S_OK ) {

                ZeroMemory( &obo,
                            sizeof(OBO_TOKEN) );

                obo.Type = OBO_USER;

                //
                // Let the network class installer prompt the user to select
                // a network component to install.
                //

                hr = pncClassSetup->SelectAndInstall( hwndDlg,
                                                      &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" installing the network component." );
                    }

                }
                else {
                    if ( hr != HRESULT_FROM_WIN32(ERROR_CANCELLED) ) {
                        ErrMsg( hr,
                                L"Couldn't install the network component." );
                    }
                }

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

        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 the get notify object interface." );
        }
    }
 
    return hr;
}

//
// Function:  InstallSpecifiedComponent
//
// Purpose:   Install a network component from an INF file.
//
// Arguments:
//    lpszInfFile [in]  INF file.
//    lpszPnpID   [in]  PnpID of the network component to install.
//    pguidClass  [in]  Class GUID of the network component.
//
// Returns:   None.
//
// Notes:
//

HRESULT InstallSpecifiedComponent (LPWSTR lpszInfFile,
                                   LPWSTR lpszPnpID,
                                   const GUID *pguidClass)
{
    INetCfg    *pnc;
    LPWSTR     lpszApp;
    HRESULT    hr;

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

    if ( hr == S_OK ) {

        //
        // Install the network component.
        //

        hr = HrInstallNetComponent( pnc,
                                    lpszPnpID,
                                    pguidClass,
                                    lpszInfFile );
        if ( (hr == S_OK) || (hr == NETCFG_S_REBOOT) ) {

            hr = pnc->Apply();
        }
        else {
            if ( hr != HRESULT_FROM_WIN32(ERROR_CANCELLED) ) {
                ErrMsg( hr,
                        L"Couldn't install the network component." );
            }
        }

        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 the get notify object interface." );
        }
    }

    return hr;
}

//
// Function:  ListCompToBindUnbind
//
// Purpose:   List all the components that are bound or bindable.
//
// Arguments:
//    lpszInfId [in]  PnpID of the network component.
//    uiType    [in]  Type of network component.
//    hwndTree  [in]  Tree handle in which to list.
//    fBound    [in]  if TRUE, list components that are bound.
//
// Returns:   Number of components listed.
//
// Notes:
//

DWORD ListCompToBindUnbind (LPWSTR lpszInfId,
                            UINT uiType,
                            HWND hwndTree,
                            BOOL fBound)
{
    INetCfg                   *pnc;
    INetCfgComponent          *pncc;
    IEnumNetCfgComponent      *pencc;
    INetCfgComponentBindings  *pnccb;
    INetCfgComponent          *pnccToBindUnbind;
    LPWSTR                    lpszApp;
    DWORD                     dwCount;
    HRESULT                   hr;


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

    if ( hr == S_OK ) {

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

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

        if ( hr == S_OK ) {

            //
            // Get Component Enumerator Interface.
            //

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

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

                    hr = HrGetFirstComponent( pencc, &pnccToBindUnbind );

                    while( hr == S_OK ) {

                        hr = pnccb->IsBoundTo( pnccToBindUnbind );

                        //
                        // fBound = TRUE ==> Want to list components that are
                        // bound.
                        //

                        if ( fBound ) {
                  
                            if ( hr == S_OK ) {
                                AddToTree( hwndTree,
                                           TVI_ROOT,
                                           pnccToBindUnbind );
                                dwCount++;
                            }
                        }
                        else {

                            //
                            // fBound = FALSE ==> Want to list components that 
                            // are not bound but are bindable.
                            //

                            if ( hr == S_FALSE ) {

                                hr = pnccb->IsBindableTo( pnccToBindUnbind );

                                if ( hr == S_OK ) {
                                    AddToTree( hwndTree,
                                               TVI_ROOT,
                                               pnccToBindUnbind );
                                    dwCount++;
                                }
                            }
                        }

                        ReleaseRef( pnccToBindUnbind );

                        hr = HrGetNextComponent( pencc, &pnccToBindUnbind );
                    }

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

                ReleaseRef( pencc );
            }
            else {
                ErrMsg( hr,
                        L"Couldn't get the network component enumerator "
                        L"interface." );
            }
  
            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." );

⌨️ 快捷键说明

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