| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the MIT License. See License.txt in the project root for license information.
- #include "precomp.h"
- HRESULT
- SetElementProperty(
- IN IAppHostElement * pElement,
- IN CONST WCHAR * szPropName,
- IN CONST VARIANT * varPropValue
- )
- {
- HRESULT hr = NOERROR;
- CComPtr<IAppHostProperty> pPropElement;
- BSTR bstrPropName = SysAllocString( szPropName );
- if( !bstrPropName )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pElement->GetPropertyByName( bstrPropName,
- &pPropElement );
- if( FAILED(hr) )
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pPropElement->put_Value( *varPropValue );
- if( FAILED(hr) )
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- exit:
- if( bstrPropName )
- {
- SysFreeString( bstrPropName );
- bstrPropName = nullptr;
- }
- return hr;
- }
- HRESULT
- SetElementStringProperty(
- IN IAppHostElement * pElement,
- IN CONST WCHAR * szPropName,
- IN CONST WCHAR * szPropValue
- )
- {
- VARIANT varPropValue;
- VariantInit(&varPropValue);
- HRESULT hr = VariantAssign(&varPropValue, szPropValue);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = SetElementProperty(pElement, szPropName, &varPropValue);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- exit:
- VariantClear(&varPropValue);
- return hr;
- }
- HRESULT
- GetElementStringProperty(
- IN IAppHostElement * pElement,
- IN CONST WCHAR * szPropName,
- OUT BSTR * pbstrPropValue
- )
- {
- HRESULT hr = S_OK;
- BSTR bstrPropName = SysAllocString( szPropName );
- IAppHostProperty* pProperty = nullptr;
- *pbstrPropValue = nullptr;
- if (!bstrPropName)
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pElement->GetPropertyByName( bstrPropName, &pProperty );
- if (FAILED(hr))
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pProperty->get_StringValue( pbstrPropValue );
- if (FAILED(hr))
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- exit:
- if (pProperty)
- {
- pProperty->Release();
- }
- if (bstrPropName)
- {
- SysFreeString( bstrPropName );
- }
- return hr;
- }
- HRESULT
- GetElementStringProperty(
- IN IAppHostElement * pElement,
- IN CONST WCHAR * szPropName,
- OUT STRU * pstrPropValue
- )
- {
- HRESULT hr = S_OK;
- BSTR bstrPropName = SysAllocString( szPropName );
- IAppHostProperty* pProperty = nullptr;
- BSTR bstrPropValue = nullptr;
- if (!bstrPropName)
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pElement->GetPropertyByName( bstrPropName, &pProperty );
- if (FAILED(hr))
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pProperty->get_StringValue( &bstrPropValue );
- if (FAILED(hr))
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = pstrPropValue->Copy(bstrPropValue);
- if (FAILED(hr))
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- exit:
- if (pProperty)
- {
- pProperty->Release();
- }
- if (bstrPropValue)
- {
- SysFreeString( bstrPropValue );
- }
- if (bstrPropName)
- {
- SysFreeString( bstrPropName );
- }
- return hr;
- }
- HRESULT
- GetElementChildByName(
- IN IAppHostElement * pElement,
- IN LPCWSTR pszElementName,
- OUT IAppHostElement ** ppChildElement
- )
- {
- BSTR bstrElementName = SysAllocString(pszElementName);
- if (bstrElementName == nullptr)
- {
- return E_OUTOFMEMORY;
- }
- HRESULT hr = pElement->GetElementByName(bstrElementName,
- ppChildElement);
- SysFreeString(bstrElementName);
- return hr;
- }
- HRESULT
- GetElementBoolProperty(
- IN IAppHostElement * pElement,
- IN LPCWSTR pszPropertyName,
- OUT bool * pBool
- )
- {
- BOOL fValue;
- HRESULT hr = GetElementBoolProperty(pElement,
- pszPropertyName,
- &fValue);
- if (SUCCEEDED(hr))
- {
- *pBool = !!fValue;
- }
- return hr;
- }
- HRESULT
- GetElementBoolProperty(
- IN IAppHostElement * pElement,
- IN LPCWSTR pszPropertyName,
- OUT BOOL * pBool
- )
- {
- HRESULT hr = S_OK;
- IAppHostProperty * pProperty = nullptr;
- VARIANT varValue;
- VariantInit( &varValue );
- BSTR bstrPropertyName = SysAllocString(pszPropertyName);
- if ( bstrPropertyName == nullptr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- // Now ask for the property and if it succeeds it is returned directly back.
- hr = pElement->GetPropertyByName( bstrPropertyName, &pProperty );
- if ( FAILED ( hr ) )
- {
- goto exit;
- }
- // Now let's get the property and then extract it from the Variant.
- hr = pProperty->get_Value( &varValue );
- if ( FAILED ( hr ) )
- {
- goto exit;
- }
- hr = VariantChangeType( &varValue, &varValue, 0, VT_BOOL );
- if ( FAILED ( hr ) )
- {
- goto exit;
- }
- // extract the value
- *pBool = ( V_BOOL( &varValue ) == VARIANT_TRUE );
- exit:
- VariantClear( &varValue );
- if ( bstrPropertyName != nullptr )
- {
- SysFreeString( bstrPropertyName );
- bstrPropertyName = nullptr;
- }
- if ( pProperty != nullptr )
- {
- pProperty->Release();
- pProperty = nullptr;
- }
- return hr;
- }
- HRESULT
- GetElementDWORDProperty(
- IN IAppHostElement * pSitesCollectionEntry,
- IN LPCWSTR pwszName,
- OUT DWORD * pdwValue
- )
- {
- HRESULT hr = S_OK;
- IAppHostProperty * pProperty = nullptr;
- VARIANT varValue;
- VariantInit( &varValue );
- BSTR bstrName = SysAllocString(pwszName);
- if ( bstrName == nullptr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto error;
- }
- hr = pSitesCollectionEntry->GetPropertyByName( bstrName,
- &pProperty );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- hr = pProperty->get_Value( &varValue );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- hr = VariantChangeType( &varValue, &varValue, 0, VT_UI4 );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- // extract the value
- *pdwValue = varValue.ulVal;
- error:
- VariantClear( &varValue );
- if ( pProperty != nullptr )
- {
- pProperty->Release();
- pProperty = nullptr;
- }
- if ( bstrName != nullptr )
- {
- SysFreeString( bstrName );
- bstrName = nullptr;
- }
- return hr;
- }
- HRESULT
- GetElementLONGLONGProperty(
- IN IAppHostElement * pSitesCollectionEntry,
- IN LPCWSTR pwszName,
- OUT LONGLONG * pllValue
- )
- {
- HRESULT hr = S_OK;
- IAppHostProperty * pProperty = nullptr;
- VARIANT varValue;
- VariantInit( &varValue );
- BSTR bstrName = SysAllocString(pwszName);
- if ( bstrName == nullptr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto error;
- }
- hr = pSitesCollectionEntry->GetPropertyByName( bstrName,
- &pProperty );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- hr = pProperty->get_Value( &varValue );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- hr = VariantChangeType( &varValue, &varValue, 0, VT_I8 );
- if ( FAILED ( hr ) )
- {
- goto error;
- }
- // extract the value
- *pllValue = varValue.ulVal;
- error:
- VariantClear( &varValue );
- if ( pProperty != nullptr )
- {
- pProperty->Release();
- pProperty = nullptr;
- }
- if ( bstrName != nullptr )
- {
- SysFreeString( bstrName );
- bstrName = nullptr;
- }
- return hr;
- }
- HRESULT
- GetElementRawTimeSpanProperty(
- IN IAppHostElement * pElement,
- IN LPCWSTR pszPropertyName,
- OUT ULONGLONG * pulonglong
- )
- {
- HRESULT hr = S_OK;
- IAppHostProperty * pProperty = nullptr;
- VARIANT varValue;
- VariantInit( &varValue );
- BSTR bstrPropertyName = SysAllocString(pszPropertyName);
- if ( bstrPropertyName == nullptr )
- {
- hr = HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
- goto Finished;
- }
- // Now ask for the property and if it succeeds it is returned directly back
- hr = pElement->GetPropertyByName( bstrPropertyName, &pProperty );
- if ( FAILED ( hr ) )
- {
- goto Finished;
- }
- // Now let's get the property and then extract it from the Variant.
- hr = pProperty->get_Value( &varValue );
- if ( FAILED ( hr ) )
- {
- goto Finished;
- }
- hr = VariantChangeType( &varValue, &varValue, 0, VT_UI8 );
- if ( FAILED ( hr ) )
- {
- goto Finished;
- }
- // extract the value
- *pulonglong = varValue.ullVal;
- Finished:
- VariantClear( &varValue );
- if ( bstrPropertyName != nullptr )
- {
- SysFreeString( bstrPropertyName );
- bstrPropertyName = nullptr;
- }
- if ( pProperty != nullptr )
- {
- pProperty->Release();
- pProperty = nullptr;
- }
- return hr;
- } // end of Config_GetRawTimeSpanProperty
- HRESULT
- DeleteElementFromCollection(
- IAppHostElementCollection *pCollection,
- CONST WCHAR * szKeyName,
- CONST WCHAR * szKeyValue,
- ULONG BehaviorFlags,
- BOOL * pfDeleted
- )
- {
- HRESULT hr = NOERROR;
- ULONG index;
- VARIANT varIndex;
- VariantInit( &varIndex );
- *pfDeleted = FALSE;
- hr = FindElementInCollection(
- pCollection,
- szKeyName,
- szKeyValue,
- BehaviorFlags,
- &index
- );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if (hr == S_FALSE)
- {
- //
- // Not found.
- //
- goto exit;
- }
- varIndex.vt = VT_UI4;
- varIndex.ulVal = index;
- hr = pCollection->DeleteElement( varIndex );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- *pfDeleted = TRUE;
- exit:
- return hr;
- }
- HRESULT
- DeleteAllElementsFromCollection(
- IAppHostElementCollection *pCollection,
- CONST WCHAR * szKeyName,
- CONST WCHAR * szKeyValue,
- ULONG BehaviorFlags,
- UINT * pNumDeleted
- )
- {
- HRESULT hr = S_OK;
- UINT numDeleted = 0;
- BOOL fDeleted = TRUE;
- while (fDeleted)
- {
- hr = DeleteElementFromCollection(
- pCollection,
- szKeyName,
- szKeyValue,
- BehaviorFlags,
- &fDeleted
- );
- if (hr == S_FALSE)
- {
- hr = S_OK;
- break;
- }
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- break;
- }
- if (fDeleted)
- {
- numDeleted++;
- }
- }
- *pNumDeleted = numDeleted;
- return hr;
- }
- BOOL
- FindCompareCaseSensitive(
- CONST WCHAR * szLookupValue,
- CONST WCHAR * szKeyValue
- )
- {
- return !wcscmp(szLookupValue, szKeyValue);
- }
- BOOL
- FindCompareCaseInsensitive(
- CONST WCHAR * szLookupValue,
- CONST WCHAR * szKeyValue
- )
- {
- return !_wcsicmp(szLookupValue, szKeyValue);
- }
- typedef
- BOOL
- (*PFN_FIND_COMPARE_PROC)(
- CONST WCHAR *szLookupValue,
- CONST WCHAR *szKeyValue
- );
- HRESULT
- FindElementInCollection(
- IAppHostElementCollection *pCollection,
- CONST WCHAR * szKeyName,
- CONST WCHAR * szKeyValue,
- ULONG BehaviorFlags,
- OUT ULONG * pIndex
- )
- {
- HRESULT hr = NOERROR;
- CComPtr<IAppHostElement> pElement;
- CComPtr<IAppHostProperty> pKeyProperty;
- VARIANT varIndex;
- VariantInit( &varIndex );
- VARIANT varKeyValue;
- VariantInit( &varKeyValue );
- DWORD count;
- DWORD i;
- BSTR bstrKeyName = nullptr;
- PFN_FIND_COMPARE_PROC compareProc;
- compareProc = (BehaviorFlags & FIND_ELEMENT_CASE_INSENSITIVE)
- ? &FindCompareCaseInsensitive
- : &FindCompareCaseSensitive;
- bstrKeyName = SysAllocString( szKeyName );
- if( !bstrKeyName )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pCollection->get_Count( &count );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- for( i = 0; i < count; i++ )
- {
- varIndex.vt = VT_UI4;
- varIndex.ulVal = i;
- hr = pCollection->get_Item( varIndex,
- &pElement );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto tryNext;
- }
- hr = pElement->GetPropertyByName( bstrKeyName,
- &pKeyProperty );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto tryNext;
- }
- hr = pKeyProperty->get_Value( &varKeyValue );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto tryNext;
- }
- if ((compareProc)(szKeyValue, varKeyValue.bstrVal))
- {
- *pIndex = i;
- break;
- }
- tryNext:
- pElement.Release();
- pKeyProperty.Release();
- VariantClear( &varKeyValue );
- }
- if (i >= count)
- {
- hr = S_FALSE;
- }
- exit:
- SysFreeString( bstrKeyName );
- VariantClear( &varKeyValue );
- return hr;
- }
- HRESULT
- VariantAssign(
- IN OUT VARIANT * pv,
- IN CONST WCHAR * sz
- )
- {
- if( !pv || !sz )
- {
- return E_INVALIDARG;
- }
- HRESULT hr = NOERROR;
- BSTR bstr = SysAllocString( sz );
- if( !bstr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR( hr );
- goto exit;
- }
- hr = VariantClear( pv );
- if( FAILED(hr) )
- {
- DBGERROR_HR( hr );
- goto exit;
- }
- pv->vt = VT_BSTR;
- pv->bstrVal = bstr;
- bstr = nullptr;
- exit:
- SysFreeString( bstr );
- return hr;
- }
- HRESULT
- GetLocationFromFile(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- IN CONST WCHAR * szLocationPath,
- OUT IAppHostConfigLocation ** ppLocation,
- OUT BOOL * pFound
- )
- {
- HRESULT hr = NOERROR;
- CComPtr<IAppHostConfigLocationCollection> pLocationCollection;
- CComPtr<IAppHostConfigLocation> pLocation;
- BSTR bstrLocationPath = nullptr;
- *ppLocation = nullptr;
- *pFound = FALSE;
- hr = GetLocationCollection( pAdminMgr,
- szConfigPath,
- &pLocationCollection );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- DWORD count;
- DWORD i;
- VARIANT varIndex;
- VariantInit( &varIndex );
- hr = pLocationCollection->get_Count( &count );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- for( i = 0; i < count; i++ )
- {
- varIndex.vt = VT_UI4;
- varIndex.ulVal = i;
- hr = pLocationCollection->get_Item( varIndex,
- &pLocation );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pLocation->get_Path( &bstrLocationPath );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if( 0 == wcscmp ( szLocationPath, bstrLocationPath ) )
- {
- *pFound = TRUE;
- *ppLocation = pLocation.Detach();
- break;
- }
- pLocation.Release();
- SysFreeString( bstrLocationPath );
- bstrLocationPath = nullptr;
- }
- exit:
- SysFreeString( bstrLocationPath );
- return hr;
- }
- HRESULT
- GetSectionFromLocation(
- IN IAppHostConfigLocation * pLocation,
- IN CONST WCHAR * szSectionName,
- OUT IAppHostElement ** ppSectionElement,
- OUT BOOL * pFound
- )
- {
- HRESULT hr = NOERROR;
- CComPtr<IAppHostElement> pSectionElement;
- DWORD count;
- DWORD i;
- VARIANT varIndex;
- VariantInit( &varIndex );
- BSTR bstrSectionName = nullptr;
- *pFound = FALSE;
- *ppSectionElement = nullptr;
- hr = pLocation->get_Count( &count );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- for( i = 0; i < count; i++ )
- {
- varIndex.vt = VT_UI4;
- varIndex.ulVal = i;
- hr = pLocation->get_Item( varIndex,
- &pSectionElement );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pSectionElement->get_Name( &bstrSectionName );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if( 0 == wcscmp ( szSectionName, bstrSectionName ) )
- {
- *pFound = TRUE;
- *ppSectionElement = pSectionElement.Detach();
- break;
- }
- pSectionElement.Release();
- SysFreeString( bstrSectionName );
- bstrSectionName = nullptr;
- }
- exit:
- SysFreeString( bstrSectionName );
- return hr;
- }
- HRESULT
- GetAdminElement(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- IN CONST WCHAR * szElementName,
- OUT IAppHostElement ** pElement
- )
- {
- HRESULT hr = S_OK;
- BSTR bstrConfigPath = SysAllocString(szConfigPath);
- BSTR bstrElementName = SysAllocString(szElementName);
- if (bstrConfigPath == nullptr || bstrElementName == nullptr)
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pAdminMgr->GetAdminSection( bstrElementName,
- bstrConfigPath,
- pElement );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- exit:
- if ( bstrElementName != nullptr )
- {
- SysFreeString(bstrElementName);
- bstrElementName = nullptr;
- }
- if ( bstrConfigPath != nullptr )
- {
- SysFreeString(bstrConfigPath);
- bstrConfigPath = nullptr;
- }
- return hr;
- }
- HRESULT
- ClearAdminElement(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- IN CONST WCHAR * szElementName
- )
- {
- CComPtr<IAppHostElement> pElement;
- HRESULT hr = GetAdminElement(
- pAdminMgr,
- szConfigPath,
- szElementName,
- &pElement
- );
- if (FAILED(hr))
- {
- if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
- {
- hr = S_OK;
- }
- else
- {
- DBGERROR_HR(hr);
- }
- goto exit;
- }
- hr = pElement->Clear();
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- exit:
- return hr;
- }
- HRESULT
- ClearElementFromAllSites(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- IN CONST WCHAR * szElementName
- )
- {
- CComPtr<IAppHostElementCollection> pSitesCollection;
- CComPtr<IAppHostElement> pSiteElement;
- CComPtr<IAppHostChildElementCollection> pChildCollection;
- ENUM_INDEX index;
- BOOL found;
- //
- // Enumerate the sites, remove the specified elements.
- //
- HRESULT hr = GetSitesCollection(
- pAdminMgr,
- szConfigPath,
- &pSitesCollection
- );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- for (hr = FindFirstElement(pSitesCollection, &index, &pSiteElement) ;
- SUCCEEDED(hr) ;
- hr = FindNextElement(pSitesCollection, &index, &pSiteElement))
- {
- if (hr == S_FALSE)
- {
- hr = S_OK;
- break;
- }
- hr = pSiteElement->get_ChildElements(&pChildCollection);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if (pChildCollection)
- {
- hr = ClearChildElementsByName(
- pChildCollection,
- szElementName,
- &found
- );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- }
- pSiteElement.Release();
- }
- exit:
- return hr;
- }
- HRESULT
- ClearElementFromAllLocations(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- IN CONST WCHAR * szElementName
- )
- {
- CComPtr<IAppHostConfigLocationCollection> pLocationCollection;
- CComPtr<IAppHostConfigLocation> pLocation;
- CComPtr<IAppHostChildElementCollection> pChildCollection;
- ENUM_INDEX index;
- //
- // Enum the <location> tags, remove the specified elements.
- //
- HRESULT hr = GetLocationCollection(
- pAdminMgr,
- szConfigPath,
- &pLocationCollection
- );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- for (hr = FindFirstLocation(pLocationCollection, &index, &pLocation) ;
- SUCCEEDED(hr) ;
- hr = FindNextLocation(pLocationCollection, &index, &pLocation))
- {
- if (hr == S_FALSE)
- {
- hr = S_OK;
- break;
- }
- hr = ClearLocationElements(pLocation, szElementName);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- pLocation.Release();
- }
- exit:
- return hr;
- }
- HRESULT
- ClearLocationElements(
- IN IAppHostConfigLocation * pLocation,
- IN CONST WCHAR * szElementName
- )
- {
- HRESULT hr;
- CComPtr<IAppHostElement> pElement;
- ENUM_INDEX index;
- BOOL matched;
- for (hr = FindFirstLocationElement(pLocation, &index, &pElement) ;
- SUCCEEDED(hr) ;
- hr = FindNextLocationElement(pLocation, &index, &pElement))
- {
- if (hr == S_FALSE)
- {
- hr = S_OK;
- break;
- }
- hr = CompareElementName(pElement, szElementName, &matched);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if (matched)
- {
- pElement->Clear();
- }
- pElement.Release();
- }
- exit:
- return hr;
- }
- HRESULT
- CompareElementName(
- IN IAppHostElement * pElement,
- IN CONST WCHAR * szNameToMatch,
- OUT BOOL * pMatched
- )
- {
- BSTR bstrElementName = nullptr;
- *pMatched = FALSE; // until proven otherwise
- HRESULT hr = pElement->get_Name(&bstrElementName);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if( 0 == wcscmp ( szNameToMatch, bstrElementName ) )
- {
- *pMatched = TRUE;
- }
- exit:
- SysFreeString(bstrElementName);
- return hr;
- }
- HRESULT
- ClearChildElementsByName(
- IN IAppHostChildElementCollection * pCollection,
- IN CONST WCHAR * szElementName,
- OUT BOOL * pFound
- )
- {
- HRESULT hr;
- CComPtr<IAppHostElement> pElement;
- ENUM_INDEX index;
- BOOL matched;
- *pFound = FALSE;
- for (hr = FindFirstChildElement(pCollection, &index, &pElement) ;
- SUCCEEDED(hr) ;
- hr = FindNextChildElement(pCollection, &index, &pElement))
- {
- if (hr == S_FALSE)
- {
- hr = S_OK;
- break;
- }
- hr = CompareElementName(pElement, szElementName, &matched);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- if (matched)
- {
- hr = pElement->Clear();
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- *pFound = TRUE;
- }
- pElement.Release();
- }
- exit:
- return hr;
- }
- HRESULT
- GetSitesCollection(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- OUT IAppHostElementCollection ** pSitesCollection
- )
- {
- HRESULT hr;
- CComPtr<IAppHostElement> pSitesElement;
- BSTR bstrConfigPath = SysAllocString(szConfigPath);
- BSTR bstrSitesSectionName = SysAllocString(L"system.applicationHost/sites");
- *pSitesCollection = nullptr;
- if (bstrConfigPath == nullptr || bstrSitesSectionName == nullptr)
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- //
- // Chase down the sites collection.
- //
- hr = pAdminMgr->GetAdminSection( bstrSitesSectionName,
- bstrConfigPath,
- &pSitesElement );
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pSitesElement->get_Collection(pSitesCollection);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- exit:
- SysFreeString(bstrSitesSectionName);
- SysFreeString(bstrConfigPath);
- return hr;
- }
- HRESULT
- GetLocationCollection(
- IN IAppHostAdminManager * pAdminMgr,
- IN CONST WCHAR * szConfigPath,
- OUT IAppHostConfigLocationCollection ** pLocationCollection
- )
- {
- HRESULT hr;
- CComPtr<IAppHostConfigManager> pConfigMgr;
- CComPtr<IAppHostConfigFile> pConfigFile;
- BSTR bstrConfigPath = SysAllocString(szConfigPath);
- *pLocationCollection = nullptr;
- if (bstrConfigPath == nullptr)
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pAdminMgr->get_ConfigManager(&pConfigMgr);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pConfigMgr->GetConfigFile(bstrConfigPath, &pConfigFile);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pConfigFile->get_Locations(pLocationCollection);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- exit:
- SysFreeString(bstrConfigPath);
- return hr;
- }
- HRESULT
- FindFirstElement(
- IN IAppHostElementCollection * pCollection,
- OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- HRESULT hr = pCollection->get_Count(&pIndex->Count);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- return hr;
- }
- VariantInit(&pIndex->Index);
- pIndex->Index.vt = VT_UI4;
- pIndex->Index.ulVal = 0;
- return FindNextElement(pCollection, pIndex, pElement);
- }
- HRESULT
- FindNextElement(
- IN IAppHostElementCollection * pCollection,
- IN OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- *pElement = nullptr;
- if (pIndex->Index.ulVal >= pIndex->Count)
- {
- return S_FALSE;
- }
- HRESULT hr = pCollection->get_Item(pIndex->Index, pElement);
- if (SUCCEEDED(hr))
- {
- pIndex->Index.ulVal++;
- }
- return hr;
- }
- HRESULT
- FindFirstChildElement(
- IN IAppHostChildElementCollection * pCollection,
- OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- HRESULT hr = pCollection->get_Count(&pIndex->Count);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- return hr;
- }
- VariantInit(&pIndex->Index);
- pIndex->Index.vt = VT_UI4;
- pIndex->Index.ulVal = 0;
- return FindNextChildElement(pCollection, pIndex, pElement);
- }
- HRESULT
- FindNextChildElement(
- IN IAppHostChildElementCollection * pCollection,
- IN OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- *pElement = nullptr;
- if (pIndex->Index.ulVal >= pIndex->Count)
- {
- return S_FALSE;
- }
- HRESULT hr = pCollection->get_Item(pIndex->Index, pElement);
- if (SUCCEEDED(hr))
- {
- pIndex->Index.ulVal++;
- }
- return hr;
- }
- HRESULT
- FindFirstLocation(
- IN IAppHostConfigLocationCollection * pCollection,
- OUT ENUM_INDEX * pIndex,
- OUT IAppHostConfigLocation ** pLocation
- )
- {
- HRESULT hr = pCollection->get_Count(&pIndex->Count);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- return hr;
- }
- VariantInit(&pIndex->Index);
- pIndex->Index.vt = VT_UI4;
- pIndex->Index.ulVal = 0;
- return FindNextLocation(pCollection, pIndex, pLocation);
- }
- HRESULT
- FindNextLocation(
- IN IAppHostConfigLocationCollection * pCollection,
- IN OUT ENUM_INDEX * pIndex,
- OUT IAppHostConfigLocation ** pLocation
- )
- {
- *pLocation = nullptr;
- if (pIndex->Index.ulVal >= pIndex->Count)
- {
- return S_FALSE;
- }
- HRESULT hr = pCollection->get_Item(pIndex->Index, pLocation);
- if (SUCCEEDED(hr))
- {
- pIndex->Index.ulVal++;
- }
- return hr;
- }
- HRESULT
- FindFirstLocationElement(
- IN IAppHostConfigLocation * pLocation,
- OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- HRESULT hr = pLocation->get_Count(&pIndex->Count);
- if (FAILED(hr))
- {
- DBGERROR_HR(hr);
- return hr;
- }
- VariantInit(&pIndex->Index);
- pIndex->Index.vt = VT_UI4;
- pIndex->Index.ulVal = 0;
- return FindNextLocationElement(pLocation, pIndex, pElement);
- }
- HRESULT
- FindNextLocationElement(
- IN IAppHostConfigLocation * pLocation,
- IN OUT ENUM_INDEX * pIndex,
- OUT IAppHostElement ** pElement
- )
- {
- *pElement = nullptr;
- if (pIndex->Index.ulVal >= pIndex->Count)
- {
- return S_FALSE;
- }
- HRESULT hr = pLocation->get_Item(pIndex->Index, pElement);
- if (SUCCEEDED(hr))
- {
- pIndex->Index.ulVal++;
- }
- return hr;
- }
- HRESULT
- GetSharedConfigEnabled(
- BOOL * pfIsSharedConfig
- )
- /*++
- Routine Description:
- Search the configuration for the shared configuration property.
- Arguments:
- pfIsSharedConfig - true if shared configuration is enabled
- Return Value:
- HRESULT
- --*/
- {
- HRESULT hr = S_OK;
- IAppHostAdminManager *pAdminManager = nullptr;
- BSTR bstrConfigPath = nullptr;
- IAppHostElement * pConfigRedirSection = nullptr;
- BSTR bstrSectionName = SysAllocString(L"configurationRedirection");
- if ( bstrSectionName == nullptr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- bstrConfigPath = SysAllocString( L"MACHINE/REDIRECTION" );
- if ( bstrConfigPath == nullptr )
- {
- hr = E_OUTOFMEMORY;
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = CoCreateInstance( CLSID_AppHostAdminManager,
- nullptr,
- CLSCTX_INPROC_SERVER,
- IID_IAppHostAdminManager,
- (VOID **)&pAdminManager );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = pAdminManager->GetAdminSection( bstrSectionName,
- bstrConfigPath,
- &pConfigRedirSection );
- if( FAILED(hr) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- hr = GetElementBoolProperty( pConfigRedirSection,
- L"enabled",
- pfIsSharedConfig );
- if ( FAILED( hr ) )
- {
- DBGERROR_HR(hr);
- goto exit;
- }
- pConfigRedirSection->Release();
- pConfigRedirSection = nullptr;
- exit:
- //
- // dump config exception to setup log file (if available)
- //
- if ( pConfigRedirSection != nullptr )
- {
- pConfigRedirSection->Release();
- }
- if ( pAdminManager != nullptr )
- {
- pAdminManager->Release();
- }
- if ( bstrConfigPath != nullptr )
- {
- SysFreeString( bstrConfigPath );
- }
- if ( bstrSectionName != nullptr )
- {
- SysFreeString( bstrSectionName );
- }
- return hr;
- }
|