IAppHostElement::ChildElements 属性

表示嵌套在当前 IAppHostElement 接口下的子元素的集合。

语法

HRESULT get_ChildElements(  
   [out,  
   retval] IAppHostChildElementCollection** ppElements  
);  

parameters

ppElements
指向 IAppHostChildElementCollection 接口的指针的指针。

返回值

HRESULT。 可能的值包括(但并不限于)下表中的项。

说明
S_OK 指示操作成功。

备注

属性中 IAppHostElement::ChildElements 表示的子元素集合不同于 IAppHostElement::Collection 属性中表示的嵌套元素集合。 这两个集合都包含指向 IAppHostElement 接口的指针。 但是,在查询配置的唯一子元素(例如system.webServer/asp配置部分)时,应使用 IAppHostElement::ChildElements 属性。 在以下配置示例中, <cache/> 元素是配置节的 system.webServer/asp 子元素。

<system.webServer>  
    <asp>  
        <cache diskTemplateCacheDirectory="%SystemDrive%\inetpub\temp\ASP Compiled Templates" />  
    </asp>  
</system.webServer>  

查询包含唯一键或多属性键的子元素(例如system.webServer/sites配置节)的配置时,应使用 IAppHostElement::Collection 属性。 在以下配置示例中, <site/> 元素是集合元素, <applicationDefaults/> 元素是配置节的 system.applicationHost/sites 子元素。

<system.applicationHost>  
    <sites>  
        <site name="Default Web Site" id="1">  
            <application path="/">  
                <virtualDirectory path="/"   
                                  physicalPath="%SystemDrive%\inetpub\wwwroot" />  
            </application>  
            <bindings>  
                <binding protocol="http" bindingInformation="*:80:" />  
            </bindings>  
        </site>  
        <site name="Microsoft.com" id="35">  
            <bindings>  
                <binding protocol="http"   
                         bindingInformation="*:80:microsoft.com" />  
            </bindings>  
        </site>  
        <siteDefaults>  
            <logFile customLogPluginClsid="{FF160663-DE82-11CF-BC0A-00AA006111E0}"   
                     directory="%SystemDrive%\inetpub\logs\LogFiles" />  
            <traceFailedRequestsLogging   
                     directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles" />  
        </siteDefaults>  
        <applicationDefaults applicationPool="DefaultAppPool" />  
        <virtualDirectoryDefaults allowSubDirConfig="true" />  
    </sites>  
</system.applicationHost>  

示例

下面的代码示例显示 MACHINE/WEBROOT/APPHOST 配置路径的配置节的所有子元素 system.webServer/asp


#pragma once

#include <stdio.h>
#include <string.h>
#include <ahadmin.h>

int main()
{
    IAppHostAdminManager            * pMgr        = NULL;
    IAppHostElement                 * pParentElem = NULL;
    IAppHostChildElementCollection  * pChildElems = NULL;
    IAppHostElement                 * pChildElem  = NULL;
    
    HRESULT    hr                = S_OK;
    BSTR    bstrSectionName      = SysAllocString( L"system.webServer/asp" );
    BSTR    bstrChildElemName    = NULL;
    DWORD   dwElementCount       = 0;

    // Initialize
    hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
    if ( FAILED( hr ) )
    {
        printf_s( "ERROR: Unable to initialize\n" );
        goto exit;
    } 

    // Create an admin manager
    hr = CoCreateInstance( __uuidof( AppHostAdminManager ), NULL, 
            CLSCTX_INPROC_SERVER,
            __uuidof( IAppHostAdminManager ), (void**) &pMgr );
    if( FAILED( hr ) )
    {
        printf_s( "ERROR: Unable to create an IAppHostAdminManager\n" );
        goto exit;
    }
    
    // Get the admin section
    wprintf_s( L"Getting %s\n", bstrSectionName );

    hr = pMgr->GetAdminSection( bstrSectionName, NULL, &pParentElem );
    if ( FAILED( hr ) || ( &pParentElem == NULL ) )
    {
        if ( E_ACCESSDENIED == hr )
        {
            printf_s( "ERROR: Access to configuration denied.\n" );
            printf_s( "       Run sample as an administrator.\n" );
        }
        else
        {
            printf_s( "ERROR: Unable to get asp configuration section.\n" );
        }
        goto exit;
    }

    // Get the child elements
    wprintf_s( L"Getting child elements\n" );
    
    hr = pParentElem->get_ChildElements( &pChildElems );
    if ( FAILED( hr ) || ( &pChildElems == NULL ) )
    {
        wprintf_s( L"ERROR: Unable to access child elements of %s\n", bstrSectionName );
        goto exit;
    }
    
    // Loop through child elements
    wprintf_s( L"Seaching for child elements of %s\n", bstrSectionName );

    hr = pChildElems->get_Count( &dwElementCount );
    for( USHORT i = 0; i < dwElementCount; i++ )
    {
        VARIANT vtItemIndex;
        vtItemIndex.vt = VT_I2;
        vtItemIndex.iVal = i;

        // Get the section group
        
        hr = pChildElems->get_Item( vtItemIndex, &pChildElem );
        if ( FAILED( hr ) || ( &pChildElem == NULL ) )
        {
            wprintf_s( L"ERROR: Unable to find child element: %d\n", i );
            goto loop_cleanup;
        }

        // Get the name

        hr = pChildElem->get_Name ( &bstrChildElemName );
        if ( FAILED( hr ) )
        {
            wprintf_s( L"ERROR: Unable to get child element name.\n" );
            goto loop_cleanup; 
        }

        wprintf_s( L"\tChild element found: %s\n", bstrChildElemName );

loop_cleanup:
        if ( pChildElem != NULL )
        {
            pChildElem->Release(); 
            pChildElem = NULL;
        }
        SysFreeString( bstrChildElemName );
    }

exit:
    // Exiting / Unwinding
    if ( pChildElems != NULL )
    {
        pChildElems->Release(); 
        pChildElems = NULL;
    }
    if ( pParentElem != NULL )
    {
        pParentElem->Release(); 
        pParentElem = NULL;
    }
    if ( pMgr != NULL )
    {
        pMgr->Release(); 
        pMgr = NULL;
    }

    SysFreeString( bstrChildElemName );
    SysFreeString( bstrSectionName );

    // Uninitialize
    CoUninitialize();

    return 0;
};

要求

类型 说明
客户端 - Windows Vista 上的 IIS 7.0
- Windows 7 上的 IIS 7.5
- Windows 8 上的 IIS 8.0
- Windows 10 上的 IIS 10.0
服务器 - Windows Server 2008 上的 IIS 7.0
- Windows Server 2008 R2 上的 IIS 7.5
- Windows Server 2012 上的 IIS 8.0
- Windows Server 2012 R2 上的 IIS 8.5
- Windows Server 2016 上的 IIS 10.0
产品 - IIS 7.0、IIS 7.5、IIS 8.0、IIS 8.5、IIS 10.0
- IIS Express 7.5、IIS Express 8.0、IIS Express 10.0
Header Ahadmin.h

另请参阅

IAppHostElement 接口
IAppHostElement::Collection 属性