IAppHostAdminManager::ConfigManager 属性

获取 一个 IAppHostConfigManager 接口,该接口提供对 IIS 7 配置系统中的物理文件的访问权限。

语法

HRESULT get_ConfigManager(  
   [out,  
   retval] IAppHostConfigManager** ppConfigManager  
);  

parameters

ppConfigManager
指向接口的指针的 IAppHostConfigManager 指针。

返回值

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

说明
S_OK 指示操作成功。
HRESULT_FROM_WIN32 (ERROR_INVALID_PARAMETER) 指示 ppConfigManager 为 NULL。
HRESULT_FROM_WIN32 (ERROR_NOT_ENOUGH_MEMORY) 指示 方法无法为返回值分配足够的内存。

示例

下面的代码示例演示接口在访问配置系统时的角色 IAppHostConfigManager


#pragma once

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

int main()
{
    HRESULT                               hr          = S_OK;
    
    IAppHostWritableAdminManager        * pWMgr       = NULL;
    IAppHostConfigManager               * pCfgMgr     = NULL;
    IAppHostConfigFile                  * pCfgFile    = NULL; 
    IAppHostSectionGroup                * pRtSctnGrp  = NULL;
    IAppHostSectionGroup                * pSctnGrp    = NULL;
    IAppHostSectionDefinitionCollection * pSctnDefCol = NULL;
    IAppHostSectionDefinition           * pSctnDef    = NULL;

    BSTR bstrConfigCommitPath = SysAllocString(L"MACHINE/WEBROOT/APPHOST");
    BSTR bstrSctnGrpName      = SysAllocString(L"mySectionGroup");
    BSTR bstrSctnName         = SysAllocString(L"myNewSection");
    BSTR bstrDeny             = SysAllocString(L"Deny");
    BSTR bstrAppHostOnly      = SysAllocString(L"appHostOnly");

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

    // Create
    hr = CoCreateInstance( __uuidof( AppHostWritableAdminManager ), NULL, 
            CLSCTX_INPROC_SERVER,
            __uuidof( IAppHostWritableAdminManager ), (void**) &pWMgr );
    if( FAILED( hr ) )
    {
        printf_s ( "ERROR: Unable to create an IAppHostWritableAdminManager\n" );
        goto exit;
    }

    pWMgr -> put_CommitPath ( bstrConfigCommitPath );

    // Get an IAppHostConfigManager
    hr = pWMgr -> get_ConfigManager ( &pCfgMgr );
    if ( FAILED( hr ) || ( &pCfgMgr == NULL ) )
    {
        printf_s ( "ERROR: Unable to get a config manager.\n" );
        goto exit;
    }
    
    // Get an IAppHostConfigFile
    hr = pCfgMgr -> GetConfigFile ( bstrConfigCommitPath, &pCfgFile );
    if ( FAILED ( hr ) || ( &pCfgFile == 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 config file.\n" );
        }
        goto exit;
    }

    // Get the root section group
    hr = pCfgFile -> get_RootSectionGroup ( &pRtSctnGrp );
    if ( FAILED ( hr ) || ( &pRtSctnGrp == NULL ) )
    {
        printf_s ( "ERROR: Unable to access root section group\n" );
        goto exit;
    }

    // Add a new section group
    hr = pRtSctnGrp -> AddSectionGroup ( bstrSctnGrpName, &pSctnGrp );
    if ( FAILED ( hr ) || ( &pSctnGrp == NULL ) )
    {
        printf_s ( "ERROR: Unable to add new section group\n" );
        goto exit;
    }

    // Get the section collection
    hr = pSctnGrp -> get_Sections ( &pSctnDefCol );
    if ( FAILED ( hr ) || ( &pSctnDefCol == NULL ) )
    {
        printf_s ( "ERROR: Unable to access section collection\n" );
        goto exit;
    }

    // Add the new section
    hr = pSctnDefCol -> AddSection ( bstrSctnName, &pSctnDef );
    if ( FAILED ( hr ) || ( &pSctnDef == NULL ) )
    {
        printf_s ( "ERROR: Unable to add new section\n" );
        goto exit;
    }

    // Set the section attributes
    pSctnDef -> put_OverrideModeDefault ( bstrDeny );
    pSctnDef -> put_AllowDefinition ( bstrAppHostOnly );

    // Commit the changes to the configuration system
    pWMgr->CommitChanges ( );

exit:
    // Exiting / Unwinding
    if ( pRtSctnGrp != NULL )
    {
        pRtSctnGrp->Release(); 
        pRtSctnGrp = NULL;
    }
    if ( pSctnGrp    != NULL )
    {
        pSctnGrp->Release(); 
        pSctnGrp = NULL;
    }
    if ( pSctnDefCol != NULL )
    {
        pSctnDefCol->Release(); 
        pSctnDefCol = NULL;
    }
    if ( pSctnDef != NULL )
    {
        pSctnDef->Release(); 
        pSctnDef = NULL;
    }
    if ( pCfgFile != NULL )
    {
        pCfgFile->Release();
        pCfgFile = NULL;
    }
    if ( pCfgMgr != NULL )
    {
        pCfgMgr->Release();
        pCfgMgr = NULL;
    }
    if ( pWMgr != NULL )
    {
        pWMgr->Release(); 
        pWMgr = NULL;
    }

    SysFreeString( bstrConfigCommitPath );
    SysFreeString( bstrSctnGrpName );
    SysFreeString( bstrSctnName );
    SysFreeString( bstrDeny );
    SysFreeString( bstrAppHostOnly );

    // 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

另请参阅

IAppHostAdminManager 接口