How to I obtain an IIS Virtual DIrectory name from

2019-08-20 19:28发布

问题:

I have the following metabase path:

/lm/w3svc/1/root/foo

which I can see in IIS manager maps to the virtual directory:

Default Web Site/foo

How can I determine the virtual directory name from the metabase path in c++?

回答1:

HRESULT CAeXMSAdminBasePtr::GetVirtualDirectoryName( LPCWSTR szMetaPath, LPWSTR  szVirtualDirectoryName, DWORD dwVirtualDirectoryNameLen )
{
    HRESULT hr = S_OK;
    METADATA_RECORD mdRecord;   
    memset(&mdRecord, 0, sizeof(METADATA_RECORD));

    METADATA_HANDLE hMetaData = NULL;
    IMSAdminBasePtr spAdminBase

    try
    {

        spAdminBase.CoCreateInstance(CLSID_MSAdminBase);

        spAdminBase->OpenKey(METADATA_MASTER_ROOT_HANDLE, szMetaPath, METADATA_PERMISSION_READ, g_dwCommandTimeOut, hMetaData);

        //
        // Get Server Comment field aka Web Site Name
        //
        MD_SET_DATA_RECORD(&mdRecord, MD_SERVER_COMMENT, METADATA_NO_ATTRIBUTES, IIS_MD_UT_SERVER, ALL_METADATA , dwVirtualDirectoryNameLen*sizeof(WCHAR), szVirtualDirectoryName );
        spAdminBase->GetData(hMetaData, L"", mdRecord, dwVirtualDirectoryNameLen ); 
        if( hMetaData != NULL )
        {
            spAdminBase->CloseKey(hMetaData);
        }
    }
    catch(...)
    {
        hr = E_FAIL;
        if( hMetaData != NULL )
        {
            spAdminBase->CloseKey(hMetaData);
        }
        // Propogate exception to caller
        throw;
    }

    return hr;
}