ahutil.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. #include "precomp.h"
  4. HRESULT
  5. SetElementProperty(
  6. IN IAppHostElement * pElement,
  7. IN CONST WCHAR * szPropName,
  8. IN CONST VARIANT * varPropValue
  9. )
  10. {
  11. HRESULT hr = NOERROR;
  12. CComPtr<IAppHostProperty> pPropElement;
  13. BSTR bstrPropName = SysAllocString( szPropName );
  14. if( !bstrPropName )
  15. {
  16. hr = E_OUTOFMEMORY;
  17. DBGERROR_HR( hr );
  18. goto exit;
  19. }
  20. hr = pElement->GetPropertyByName( bstrPropName,
  21. &pPropElement );
  22. if( FAILED(hr) )
  23. {
  24. DBGERROR_HR( hr );
  25. goto exit;
  26. }
  27. hr = pPropElement->put_Value( *varPropValue );
  28. if( FAILED(hr) )
  29. {
  30. DBGERROR_HR( hr );
  31. goto exit;
  32. }
  33. exit:
  34. if( bstrPropName )
  35. {
  36. SysFreeString( bstrPropName );
  37. bstrPropName = nullptr;
  38. }
  39. return hr;
  40. }
  41. HRESULT
  42. SetElementStringProperty(
  43. IN IAppHostElement * pElement,
  44. IN CONST WCHAR * szPropName,
  45. IN CONST WCHAR * szPropValue
  46. )
  47. {
  48. VARIANT varPropValue;
  49. VariantInit(&varPropValue);
  50. HRESULT hr = VariantAssign(&varPropValue, szPropValue);
  51. if (FAILED(hr))
  52. {
  53. DBGERROR_HR(hr);
  54. goto exit;
  55. }
  56. hr = SetElementProperty(pElement, szPropName, &varPropValue);
  57. if (FAILED(hr))
  58. {
  59. DBGERROR_HR(hr);
  60. goto exit;
  61. }
  62. exit:
  63. VariantClear(&varPropValue);
  64. return hr;
  65. }
  66. HRESULT
  67. GetElementStringProperty(
  68. IN IAppHostElement * pElement,
  69. IN CONST WCHAR * szPropName,
  70. OUT BSTR * pbstrPropValue
  71. )
  72. {
  73. HRESULT hr = S_OK;
  74. BSTR bstrPropName = SysAllocString( szPropName );
  75. IAppHostProperty* pProperty = nullptr;
  76. *pbstrPropValue = nullptr;
  77. if (!bstrPropName)
  78. {
  79. hr = E_OUTOFMEMORY;
  80. DBGERROR_HR( hr );
  81. goto exit;
  82. }
  83. hr = pElement->GetPropertyByName( bstrPropName, &pProperty );
  84. if (FAILED(hr))
  85. {
  86. DBGERROR_HR( hr );
  87. goto exit;
  88. }
  89. hr = pProperty->get_StringValue( pbstrPropValue );
  90. if (FAILED(hr))
  91. {
  92. DBGERROR_HR( hr );
  93. goto exit;
  94. }
  95. exit:
  96. if (pProperty)
  97. {
  98. pProperty->Release();
  99. }
  100. if (bstrPropName)
  101. {
  102. SysFreeString( bstrPropName );
  103. }
  104. return hr;
  105. }
  106. HRESULT
  107. GetElementStringProperty(
  108. IN IAppHostElement * pElement,
  109. IN CONST WCHAR * szPropName,
  110. OUT STRU * pstrPropValue
  111. )
  112. {
  113. HRESULT hr = S_OK;
  114. BSTR bstrPropName = SysAllocString( szPropName );
  115. IAppHostProperty* pProperty = nullptr;
  116. BSTR bstrPropValue = nullptr;
  117. if (!bstrPropName)
  118. {
  119. hr = E_OUTOFMEMORY;
  120. DBGERROR_HR( hr );
  121. goto exit;
  122. }
  123. hr = pElement->GetPropertyByName( bstrPropName, &pProperty );
  124. if (FAILED(hr))
  125. {
  126. DBGERROR_HR( hr );
  127. goto exit;
  128. }
  129. hr = pProperty->get_StringValue( &bstrPropValue );
  130. if (FAILED(hr))
  131. {
  132. DBGERROR_HR( hr );
  133. goto exit;
  134. }
  135. hr = pstrPropValue->Copy(bstrPropValue);
  136. if (FAILED(hr))
  137. {
  138. DBGERROR_HR( hr );
  139. goto exit;
  140. }
  141. exit:
  142. if (pProperty)
  143. {
  144. pProperty->Release();
  145. }
  146. if (bstrPropValue)
  147. {
  148. SysFreeString( bstrPropValue );
  149. }
  150. if (bstrPropName)
  151. {
  152. SysFreeString( bstrPropName );
  153. }
  154. return hr;
  155. }
  156. HRESULT
  157. GetElementChildByName(
  158. IN IAppHostElement * pElement,
  159. IN LPCWSTR pszElementName,
  160. OUT IAppHostElement ** ppChildElement
  161. )
  162. {
  163. BSTR bstrElementName = SysAllocString(pszElementName);
  164. if (bstrElementName == nullptr)
  165. {
  166. return E_OUTOFMEMORY;
  167. }
  168. HRESULT hr = pElement->GetElementByName(bstrElementName,
  169. ppChildElement);
  170. SysFreeString(bstrElementName);
  171. return hr;
  172. }
  173. HRESULT
  174. GetElementBoolProperty(
  175. IN IAppHostElement * pElement,
  176. IN LPCWSTR pszPropertyName,
  177. OUT bool * pBool
  178. )
  179. {
  180. BOOL fValue;
  181. HRESULT hr = GetElementBoolProperty(pElement,
  182. pszPropertyName,
  183. &fValue);
  184. if (SUCCEEDED(hr))
  185. {
  186. *pBool = !!fValue;
  187. }
  188. return hr;
  189. }
  190. HRESULT
  191. GetElementBoolProperty(
  192. IN IAppHostElement * pElement,
  193. IN LPCWSTR pszPropertyName,
  194. OUT BOOL * pBool
  195. )
  196. {
  197. HRESULT hr = S_OK;
  198. IAppHostProperty * pProperty = nullptr;
  199. VARIANT varValue;
  200. VariantInit( &varValue );
  201. BSTR bstrPropertyName = SysAllocString(pszPropertyName);
  202. if ( bstrPropertyName == nullptr )
  203. {
  204. hr = E_OUTOFMEMORY;
  205. DBGERROR_HR(hr);
  206. goto exit;
  207. }
  208. // Now ask for the property and if it succeeds it is returned directly back.
  209. hr = pElement->GetPropertyByName( bstrPropertyName, &pProperty );
  210. if ( FAILED ( hr ) )
  211. {
  212. goto exit;
  213. }
  214. // Now let's get the property and then extract it from the Variant.
  215. hr = pProperty->get_Value( &varValue );
  216. if ( FAILED ( hr ) )
  217. {
  218. goto exit;
  219. }
  220. hr = VariantChangeType( &varValue, &varValue, 0, VT_BOOL );
  221. if ( FAILED ( hr ) )
  222. {
  223. goto exit;
  224. }
  225. // extract the value
  226. *pBool = ( V_BOOL( &varValue ) == VARIANT_TRUE );
  227. exit:
  228. VariantClear( &varValue );
  229. if ( bstrPropertyName != nullptr )
  230. {
  231. SysFreeString( bstrPropertyName );
  232. bstrPropertyName = nullptr;
  233. }
  234. if ( pProperty != nullptr )
  235. {
  236. pProperty->Release();
  237. pProperty = nullptr;
  238. }
  239. return hr;
  240. }
  241. HRESULT
  242. GetElementDWORDProperty(
  243. IN IAppHostElement * pSitesCollectionEntry,
  244. IN LPCWSTR pwszName,
  245. OUT DWORD * pdwValue
  246. )
  247. {
  248. HRESULT hr = S_OK;
  249. IAppHostProperty * pProperty = nullptr;
  250. VARIANT varValue;
  251. VariantInit( &varValue );
  252. BSTR bstrName = SysAllocString(pwszName);
  253. if ( bstrName == nullptr )
  254. {
  255. hr = E_OUTOFMEMORY;
  256. DBGERROR_HR(hr);
  257. goto error;
  258. }
  259. hr = pSitesCollectionEntry->GetPropertyByName( bstrName,
  260. &pProperty );
  261. if ( FAILED ( hr ) )
  262. {
  263. goto error;
  264. }
  265. hr = pProperty->get_Value( &varValue );
  266. if ( FAILED ( hr ) )
  267. {
  268. goto error;
  269. }
  270. hr = VariantChangeType( &varValue, &varValue, 0, VT_UI4 );
  271. if ( FAILED ( hr ) )
  272. {
  273. goto error;
  274. }
  275. // extract the value
  276. *pdwValue = varValue.ulVal;
  277. error:
  278. VariantClear( &varValue );
  279. if ( pProperty != nullptr )
  280. {
  281. pProperty->Release();
  282. pProperty = nullptr;
  283. }
  284. if ( bstrName != nullptr )
  285. {
  286. SysFreeString( bstrName );
  287. bstrName = nullptr;
  288. }
  289. return hr;
  290. }
  291. HRESULT
  292. GetElementLONGLONGProperty(
  293. IN IAppHostElement * pSitesCollectionEntry,
  294. IN LPCWSTR pwszName,
  295. OUT LONGLONG * pllValue
  296. )
  297. {
  298. HRESULT hr = S_OK;
  299. IAppHostProperty * pProperty = nullptr;
  300. VARIANT varValue;
  301. VariantInit( &varValue );
  302. BSTR bstrName = SysAllocString(pwszName);
  303. if ( bstrName == nullptr )
  304. {
  305. hr = E_OUTOFMEMORY;
  306. DBGERROR_HR(hr);
  307. goto error;
  308. }
  309. hr = pSitesCollectionEntry->GetPropertyByName( bstrName,
  310. &pProperty );
  311. if ( FAILED ( hr ) )
  312. {
  313. goto error;
  314. }
  315. hr = pProperty->get_Value( &varValue );
  316. if ( FAILED ( hr ) )
  317. {
  318. goto error;
  319. }
  320. hr = VariantChangeType( &varValue, &varValue, 0, VT_I8 );
  321. if ( FAILED ( hr ) )
  322. {
  323. goto error;
  324. }
  325. // extract the value
  326. *pllValue = varValue.ulVal;
  327. error:
  328. VariantClear( &varValue );
  329. if ( pProperty != nullptr )
  330. {
  331. pProperty->Release();
  332. pProperty = nullptr;
  333. }
  334. if ( bstrName != nullptr )
  335. {
  336. SysFreeString( bstrName );
  337. bstrName = nullptr;
  338. }
  339. return hr;
  340. }
  341. HRESULT
  342. GetElementRawTimeSpanProperty(
  343. IN IAppHostElement * pElement,
  344. IN LPCWSTR pszPropertyName,
  345. OUT ULONGLONG * pulonglong
  346. )
  347. {
  348. HRESULT hr = S_OK;
  349. IAppHostProperty * pProperty = nullptr;
  350. VARIANT varValue;
  351. VariantInit( &varValue );
  352. BSTR bstrPropertyName = SysAllocString(pszPropertyName);
  353. if ( bstrPropertyName == nullptr )
  354. {
  355. hr = HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
  356. goto Finished;
  357. }
  358. // Now ask for the property and if it succeeds it is returned directly back
  359. hr = pElement->GetPropertyByName( bstrPropertyName, &pProperty );
  360. if ( FAILED ( hr ) )
  361. {
  362. goto Finished;
  363. }
  364. // Now let's get the property and then extract it from the Variant.
  365. hr = pProperty->get_Value( &varValue );
  366. if ( FAILED ( hr ) )
  367. {
  368. goto Finished;
  369. }
  370. hr = VariantChangeType( &varValue, &varValue, 0, VT_UI8 );
  371. if ( FAILED ( hr ) )
  372. {
  373. goto Finished;
  374. }
  375. // extract the value
  376. *pulonglong = varValue.ullVal;
  377. Finished:
  378. VariantClear( &varValue );
  379. if ( bstrPropertyName != nullptr )
  380. {
  381. SysFreeString( bstrPropertyName );
  382. bstrPropertyName = nullptr;
  383. }
  384. if ( pProperty != nullptr )
  385. {
  386. pProperty->Release();
  387. pProperty = nullptr;
  388. }
  389. return hr;
  390. } // end of Config_GetRawTimeSpanProperty
  391. HRESULT
  392. DeleteElementFromCollection(
  393. IAppHostElementCollection *pCollection,
  394. CONST WCHAR * szKeyName,
  395. CONST WCHAR * szKeyValue,
  396. ULONG BehaviorFlags,
  397. BOOL * pfDeleted
  398. )
  399. {
  400. HRESULT hr = NOERROR;
  401. ULONG index;
  402. VARIANT varIndex;
  403. VariantInit( &varIndex );
  404. *pfDeleted = FALSE;
  405. hr = FindElementInCollection(
  406. pCollection,
  407. szKeyName,
  408. szKeyValue,
  409. BehaviorFlags,
  410. &index
  411. );
  412. if (FAILED(hr))
  413. {
  414. DBGERROR_HR(hr);
  415. goto exit;
  416. }
  417. if (hr == S_FALSE)
  418. {
  419. //
  420. // Not found.
  421. //
  422. goto exit;
  423. }
  424. varIndex.vt = VT_UI4;
  425. varIndex.ulVal = index;
  426. hr = pCollection->DeleteElement( varIndex );
  427. if( FAILED(hr) )
  428. {
  429. DBGERROR_HR(hr);
  430. goto exit;
  431. }
  432. *pfDeleted = TRUE;
  433. exit:
  434. return hr;
  435. }
  436. HRESULT
  437. DeleteAllElementsFromCollection(
  438. IAppHostElementCollection *pCollection,
  439. CONST WCHAR * szKeyName,
  440. CONST WCHAR * szKeyValue,
  441. ULONG BehaviorFlags,
  442. UINT * pNumDeleted
  443. )
  444. {
  445. HRESULT hr = S_OK;
  446. UINT numDeleted = 0;
  447. BOOL fDeleted = TRUE;
  448. while (fDeleted)
  449. {
  450. hr = DeleteElementFromCollection(
  451. pCollection,
  452. szKeyName,
  453. szKeyValue,
  454. BehaviorFlags,
  455. &fDeleted
  456. );
  457. if (hr == S_FALSE)
  458. {
  459. hr = S_OK;
  460. break;
  461. }
  462. if (FAILED(hr))
  463. {
  464. DBGERROR_HR(hr);
  465. break;
  466. }
  467. if (fDeleted)
  468. {
  469. numDeleted++;
  470. }
  471. }
  472. *pNumDeleted = numDeleted;
  473. return hr;
  474. }
  475. BOOL
  476. FindCompareCaseSensitive(
  477. CONST WCHAR * szLookupValue,
  478. CONST WCHAR * szKeyValue
  479. )
  480. {
  481. return !wcscmp(szLookupValue, szKeyValue);
  482. }
  483. BOOL
  484. FindCompareCaseInsensitive(
  485. CONST WCHAR * szLookupValue,
  486. CONST WCHAR * szKeyValue
  487. )
  488. {
  489. return !_wcsicmp(szLookupValue, szKeyValue);
  490. }
  491. typedef
  492. BOOL
  493. (*PFN_FIND_COMPARE_PROC)(
  494. CONST WCHAR *szLookupValue,
  495. CONST WCHAR *szKeyValue
  496. );
  497. HRESULT
  498. FindElementInCollection(
  499. IAppHostElementCollection *pCollection,
  500. CONST WCHAR * szKeyName,
  501. CONST WCHAR * szKeyValue,
  502. ULONG BehaviorFlags,
  503. OUT ULONG * pIndex
  504. )
  505. {
  506. HRESULT hr = NOERROR;
  507. CComPtr<IAppHostElement> pElement;
  508. CComPtr<IAppHostProperty> pKeyProperty;
  509. VARIANT varIndex;
  510. VariantInit( &varIndex );
  511. VARIANT varKeyValue;
  512. VariantInit( &varKeyValue );
  513. DWORD count;
  514. DWORD i;
  515. BSTR bstrKeyName = nullptr;
  516. PFN_FIND_COMPARE_PROC compareProc;
  517. compareProc = (BehaviorFlags & FIND_ELEMENT_CASE_INSENSITIVE)
  518. ? &FindCompareCaseInsensitive
  519. : &FindCompareCaseSensitive;
  520. bstrKeyName = SysAllocString( szKeyName );
  521. if( !bstrKeyName )
  522. {
  523. hr = E_OUTOFMEMORY;
  524. DBGERROR_HR(hr);
  525. goto exit;
  526. }
  527. hr = pCollection->get_Count( &count );
  528. if( FAILED(hr) )
  529. {
  530. DBGERROR_HR(hr);
  531. goto exit;
  532. }
  533. for( i = 0; i < count; i++ )
  534. {
  535. varIndex.vt = VT_UI4;
  536. varIndex.ulVal = i;
  537. hr = pCollection->get_Item( varIndex,
  538. &pElement );
  539. if( FAILED(hr) )
  540. {
  541. DBGERROR_HR(hr);
  542. goto tryNext;
  543. }
  544. hr = pElement->GetPropertyByName( bstrKeyName,
  545. &pKeyProperty );
  546. if( FAILED(hr) )
  547. {
  548. DBGERROR_HR(hr);
  549. goto tryNext;
  550. }
  551. hr = pKeyProperty->get_Value( &varKeyValue );
  552. if( FAILED(hr) )
  553. {
  554. DBGERROR_HR(hr);
  555. goto tryNext;
  556. }
  557. if ((compareProc)(szKeyValue, varKeyValue.bstrVal))
  558. {
  559. *pIndex = i;
  560. break;
  561. }
  562. tryNext:
  563. pElement.Release();
  564. pKeyProperty.Release();
  565. VariantClear( &varKeyValue );
  566. }
  567. if (i >= count)
  568. {
  569. hr = S_FALSE;
  570. }
  571. exit:
  572. SysFreeString( bstrKeyName );
  573. VariantClear( &varKeyValue );
  574. return hr;
  575. }
  576. HRESULT
  577. VariantAssign(
  578. IN OUT VARIANT * pv,
  579. IN CONST WCHAR * sz
  580. )
  581. {
  582. if( !pv || !sz )
  583. {
  584. return E_INVALIDARG;
  585. }
  586. HRESULT hr = NOERROR;
  587. BSTR bstr = SysAllocString( sz );
  588. if( !bstr )
  589. {
  590. hr = E_OUTOFMEMORY;
  591. DBGERROR_HR( hr );
  592. goto exit;
  593. }
  594. hr = VariantClear( pv );
  595. if( FAILED(hr) )
  596. {
  597. DBGERROR_HR( hr );
  598. goto exit;
  599. }
  600. pv->vt = VT_BSTR;
  601. pv->bstrVal = bstr;
  602. bstr = nullptr;
  603. exit:
  604. SysFreeString( bstr );
  605. return hr;
  606. }
  607. HRESULT
  608. GetLocationFromFile(
  609. IN IAppHostAdminManager * pAdminMgr,
  610. IN CONST WCHAR * szConfigPath,
  611. IN CONST WCHAR * szLocationPath,
  612. OUT IAppHostConfigLocation ** ppLocation,
  613. OUT BOOL * pFound
  614. )
  615. {
  616. HRESULT hr = NOERROR;
  617. CComPtr<IAppHostConfigLocationCollection> pLocationCollection;
  618. CComPtr<IAppHostConfigLocation> pLocation;
  619. BSTR bstrLocationPath = nullptr;
  620. *ppLocation = nullptr;
  621. *pFound = FALSE;
  622. hr = GetLocationCollection( pAdminMgr,
  623. szConfigPath,
  624. &pLocationCollection );
  625. if( FAILED(hr) )
  626. {
  627. DBGERROR_HR(hr);
  628. goto exit;
  629. }
  630. DWORD count;
  631. DWORD i;
  632. VARIANT varIndex;
  633. VariantInit( &varIndex );
  634. hr = pLocationCollection->get_Count( &count );
  635. if( FAILED(hr) )
  636. {
  637. DBGERROR_HR(hr);
  638. goto exit;
  639. }
  640. for( i = 0; i < count; i++ )
  641. {
  642. varIndex.vt = VT_UI4;
  643. varIndex.ulVal = i;
  644. hr = pLocationCollection->get_Item( varIndex,
  645. &pLocation );
  646. if( FAILED(hr) )
  647. {
  648. DBGERROR_HR(hr);
  649. goto exit;
  650. }
  651. hr = pLocation->get_Path( &bstrLocationPath );
  652. if( FAILED(hr) )
  653. {
  654. DBGERROR_HR(hr);
  655. goto exit;
  656. }
  657. if( 0 == wcscmp ( szLocationPath, bstrLocationPath ) )
  658. {
  659. *pFound = TRUE;
  660. *ppLocation = pLocation.Detach();
  661. break;
  662. }
  663. pLocation.Release();
  664. SysFreeString( bstrLocationPath );
  665. bstrLocationPath = nullptr;
  666. }
  667. exit:
  668. SysFreeString( bstrLocationPath );
  669. return hr;
  670. }
  671. HRESULT
  672. GetSectionFromLocation(
  673. IN IAppHostConfigLocation * pLocation,
  674. IN CONST WCHAR * szSectionName,
  675. OUT IAppHostElement ** ppSectionElement,
  676. OUT BOOL * pFound
  677. )
  678. {
  679. HRESULT hr = NOERROR;
  680. CComPtr<IAppHostElement> pSectionElement;
  681. DWORD count;
  682. DWORD i;
  683. VARIANT varIndex;
  684. VariantInit( &varIndex );
  685. BSTR bstrSectionName = nullptr;
  686. *pFound = FALSE;
  687. *ppSectionElement = nullptr;
  688. hr = pLocation->get_Count( &count );
  689. if( FAILED(hr) )
  690. {
  691. DBGERROR_HR(hr);
  692. goto exit;
  693. }
  694. for( i = 0; i < count; i++ )
  695. {
  696. varIndex.vt = VT_UI4;
  697. varIndex.ulVal = i;
  698. hr = pLocation->get_Item( varIndex,
  699. &pSectionElement );
  700. if( FAILED(hr) )
  701. {
  702. DBGERROR_HR(hr);
  703. goto exit;
  704. }
  705. hr = pSectionElement->get_Name( &bstrSectionName );
  706. if( FAILED(hr) )
  707. {
  708. DBGERROR_HR(hr);
  709. goto exit;
  710. }
  711. if( 0 == wcscmp ( szSectionName, bstrSectionName ) )
  712. {
  713. *pFound = TRUE;
  714. *ppSectionElement = pSectionElement.Detach();
  715. break;
  716. }
  717. pSectionElement.Release();
  718. SysFreeString( bstrSectionName );
  719. bstrSectionName = nullptr;
  720. }
  721. exit:
  722. SysFreeString( bstrSectionName );
  723. return hr;
  724. }
  725. HRESULT
  726. GetAdminElement(
  727. IN IAppHostAdminManager * pAdminMgr,
  728. IN CONST WCHAR * szConfigPath,
  729. IN CONST WCHAR * szElementName,
  730. OUT IAppHostElement ** pElement
  731. )
  732. {
  733. HRESULT hr = S_OK;
  734. BSTR bstrConfigPath = SysAllocString(szConfigPath);
  735. BSTR bstrElementName = SysAllocString(szElementName);
  736. if (bstrConfigPath == nullptr || bstrElementName == nullptr)
  737. {
  738. hr = E_OUTOFMEMORY;
  739. DBGERROR_HR(hr);
  740. goto exit;
  741. }
  742. hr = pAdminMgr->GetAdminSection( bstrElementName,
  743. bstrConfigPath,
  744. pElement );
  745. if (FAILED(hr))
  746. {
  747. DBGERROR_HR(hr);
  748. goto exit;
  749. }
  750. exit:
  751. if ( bstrElementName != nullptr )
  752. {
  753. SysFreeString(bstrElementName);
  754. bstrElementName = nullptr;
  755. }
  756. if ( bstrConfigPath != nullptr )
  757. {
  758. SysFreeString(bstrConfigPath);
  759. bstrConfigPath = nullptr;
  760. }
  761. return hr;
  762. }
  763. HRESULT
  764. ClearAdminElement(
  765. IN IAppHostAdminManager * pAdminMgr,
  766. IN CONST WCHAR * szConfigPath,
  767. IN CONST WCHAR * szElementName
  768. )
  769. {
  770. CComPtr<IAppHostElement> pElement;
  771. HRESULT hr = GetAdminElement(
  772. pAdminMgr,
  773. szConfigPath,
  774. szElementName,
  775. &pElement
  776. );
  777. if (FAILED(hr))
  778. {
  779. if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
  780. {
  781. hr = S_OK;
  782. }
  783. else
  784. {
  785. DBGERROR_HR(hr);
  786. }
  787. goto exit;
  788. }
  789. hr = pElement->Clear();
  790. if (FAILED(hr))
  791. {
  792. DBGERROR_HR(hr);
  793. goto exit;
  794. }
  795. exit:
  796. return hr;
  797. }
  798. HRESULT
  799. ClearElementFromAllSites(
  800. IN IAppHostAdminManager * pAdminMgr,
  801. IN CONST WCHAR * szConfigPath,
  802. IN CONST WCHAR * szElementName
  803. )
  804. {
  805. CComPtr<IAppHostElementCollection> pSitesCollection;
  806. CComPtr<IAppHostElement> pSiteElement;
  807. CComPtr<IAppHostChildElementCollection> pChildCollection;
  808. ENUM_INDEX index;
  809. BOOL found;
  810. //
  811. // Enumerate the sites, remove the specified elements.
  812. //
  813. HRESULT hr = GetSitesCollection(
  814. pAdminMgr,
  815. szConfigPath,
  816. &pSitesCollection
  817. );
  818. if (FAILED(hr))
  819. {
  820. DBGERROR_HR(hr);
  821. goto exit;
  822. }
  823. for (hr = FindFirstElement(pSitesCollection, &index, &pSiteElement) ;
  824. SUCCEEDED(hr) ;
  825. hr = FindNextElement(pSitesCollection, &index, &pSiteElement))
  826. {
  827. if (hr == S_FALSE)
  828. {
  829. hr = S_OK;
  830. break;
  831. }
  832. hr = pSiteElement->get_ChildElements(&pChildCollection);
  833. if (FAILED(hr))
  834. {
  835. DBGERROR_HR(hr);
  836. goto exit;
  837. }
  838. if (pChildCollection)
  839. {
  840. hr = ClearChildElementsByName(
  841. pChildCollection,
  842. szElementName,
  843. &found
  844. );
  845. if (FAILED(hr))
  846. {
  847. DBGERROR_HR(hr);
  848. goto exit;
  849. }
  850. }
  851. pSiteElement.Release();
  852. }
  853. exit:
  854. return hr;
  855. }
  856. HRESULT
  857. ClearElementFromAllLocations(
  858. IN IAppHostAdminManager * pAdminMgr,
  859. IN CONST WCHAR * szConfigPath,
  860. IN CONST WCHAR * szElementName
  861. )
  862. {
  863. CComPtr<IAppHostConfigLocationCollection> pLocationCollection;
  864. CComPtr<IAppHostConfigLocation> pLocation;
  865. CComPtr<IAppHostChildElementCollection> pChildCollection;
  866. ENUM_INDEX index;
  867. //
  868. // Enum the <location> tags, remove the specified elements.
  869. //
  870. HRESULT hr = GetLocationCollection(
  871. pAdminMgr,
  872. szConfigPath,
  873. &pLocationCollection
  874. );
  875. if (FAILED(hr))
  876. {
  877. DBGERROR_HR(hr);
  878. goto exit;
  879. }
  880. for (hr = FindFirstLocation(pLocationCollection, &index, &pLocation) ;
  881. SUCCEEDED(hr) ;
  882. hr = FindNextLocation(pLocationCollection, &index, &pLocation))
  883. {
  884. if (hr == S_FALSE)
  885. {
  886. hr = S_OK;
  887. break;
  888. }
  889. hr = ClearLocationElements(pLocation, szElementName);
  890. if (FAILED(hr))
  891. {
  892. DBGERROR_HR(hr);
  893. goto exit;
  894. }
  895. pLocation.Release();
  896. }
  897. exit:
  898. return hr;
  899. }
  900. HRESULT
  901. ClearLocationElements(
  902. IN IAppHostConfigLocation * pLocation,
  903. IN CONST WCHAR * szElementName
  904. )
  905. {
  906. HRESULT hr;
  907. CComPtr<IAppHostElement> pElement;
  908. ENUM_INDEX index;
  909. BOOL matched;
  910. for (hr = FindFirstLocationElement(pLocation, &index, &pElement) ;
  911. SUCCEEDED(hr) ;
  912. hr = FindNextLocationElement(pLocation, &index, &pElement))
  913. {
  914. if (hr == S_FALSE)
  915. {
  916. hr = S_OK;
  917. break;
  918. }
  919. hr = CompareElementName(pElement, szElementName, &matched);
  920. if (FAILED(hr))
  921. {
  922. DBGERROR_HR(hr);
  923. goto exit;
  924. }
  925. if (matched)
  926. {
  927. pElement->Clear();
  928. }
  929. pElement.Release();
  930. }
  931. exit:
  932. return hr;
  933. }
  934. HRESULT
  935. CompareElementName(
  936. IN IAppHostElement * pElement,
  937. IN CONST WCHAR * szNameToMatch,
  938. OUT BOOL * pMatched
  939. )
  940. {
  941. BSTR bstrElementName = nullptr;
  942. *pMatched = FALSE; // until proven otherwise
  943. HRESULT hr = pElement->get_Name(&bstrElementName);
  944. if (FAILED(hr))
  945. {
  946. DBGERROR_HR(hr);
  947. goto exit;
  948. }
  949. if( 0 == wcscmp ( szNameToMatch, bstrElementName ) )
  950. {
  951. *pMatched = TRUE;
  952. }
  953. exit:
  954. SysFreeString(bstrElementName);
  955. return hr;
  956. }
  957. HRESULT
  958. ClearChildElementsByName(
  959. IN IAppHostChildElementCollection * pCollection,
  960. IN CONST WCHAR * szElementName,
  961. OUT BOOL * pFound
  962. )
  963. {
  964. HRESULT hr;
  965. CComPtr<IAppHostElement> pElement;
  966. ENUM_INDEX index;
  967. BOOL matched;
  968. *pFound = FALSE;
  969. for (hr = FindFirstChildElement(pCollection, &index, &pElement) ;
  970. SUCCEEDED(hr) ;
  971. hr = FindNextChildElement(pCollection, &index, &pElement))
  972. {
  973. if (hr == S_FALSE)
  974. {
  975. hr = S_OK;
  976. break;
  977. }
  978. hr = CompareElementName(pElement, szElementName, &matched);
  979. if (FAILED(hr))
  980. {
  981. DBGERROR_HR(hr);
  982. goto exit;
  983. }
  984. if (matched)
  985. {
  986. hr = pElement->Clear();
  987. if (FAILED(hr))
  988. {
  989. DBGERROR_HR(hr);
  990. goto exit;
  991. }
  992. *pFound = TRUE;
  993. }
  994. pElement.Release();
  995. }
  996. exit:
  997. return hr;
  998. }
  999. HRESULT
  1000. GetSitesCollection(
  1001. IN IAppHostAdminManager * pAdminMgr,
  1002. IN CONST WCHAR * szConfigPath,
  1003. OUT IAppHostElementCollection ** pSitesCollection
  1004. )
  1005. {
  1006. HRESULT hr;
  1007. CComPtr<IAppHostElement> pSitesElement;
  1008. BSTR bstrConfigPath = SysAllocString(szConfigPath);
  1009. BSTR bstrSitesSectionName = SysAllocString(L"system.applicationHost/sites");
  1010. *pSitesCollection = nullptr;
  1011. if (bstrConfigPath == nullptr || bstrSitesSectionName == nullptr)
  1012. {
  1013. hr = E_OUTOFMEMORY;
  1014. DBGERROR_HR(hr);
  1015. goto exit;
  1016. }
  1017. //
  1018. // Chase down the sites collection.
  1019. //
  1020. hr = pAdminMgr->GetAdminSection( bstrSitesSectionName,
  1021. bstrConfigPath,
  1022. &pSitesElement );
  1023. if (FAILED(hr))
  1024. {
  1025. DBGERROR_HR(hr);
  1026. goto exit;
  1027. }
  1028. hr = pSitesElement->get_Collection(pSitesCollection);
  1029. if (FAILED(hr))
  1030. {
  1031. DBGERROR_HR(hr);
  1032. goto exit;
  1033. }
  1034. exit:
  1035. SysFreeString(bstrSitesSectionName);
  1036. SysFreeString(bstrConfigPath);
  1037. return hr;
  1038. }
  1039. HRESULT
  1040. GetLocationCollection(
  1041. IN IAppHostAdminManager * pAdminMgr,
  1042. IN CONST WCHAR * szConfigPath,
  1043. OUT IAppHostConfigLocationCollection ** pLocationCollection
  1044. )
  1045. {
  1046. HRESULT hr;
  1047. CComPtr<IAppHostConfigManager> pConfigMgr;
  1048. CComPtr<IAppHostConfigFile> pConfigFile;
  1049. BSTR bstrConfigPath = SysAllocString(szConfigPath);
  1050. *pLocationCollection = nullptr;
  1051. if (bstrConfigPath == nullptr)
  1052. {
  1053. hr = E_OUTOFMEMORY;
  1054. DBGERROR_HR(hr);
  1055. goto exit;
  1056. }
  1057. hr = pAdminMgr->get_ConfigManager(&pConfigMgr);
  1058. if (FAILED(hr))
  1059. {
  1060. DBGERROR_HR(hr);
  1061. goto exit;
  1062. }
  1063. hr = pConfigMgr->GetConfigFile(bstrConfigPath, &pConfigFile);
  1064. if (FAILED(hr))
  1065. {
  1066. DBGERROR_HR(hr);
  1067. goto exit;
  1068. }
  1069. hr = pConfigFile->get_Locations(pLocationCollection);
  1070. if (FAILED(hr))
  1071. {
  1072. DBGERROR_HR(hr);
  1073. goto exit;
  1074. }
  1075. exit:
  1076. SysFreeString(bstrConfigPath);
  1077. return hr;
  1078. }
  1079. HRESULT
  1080. FindFirstElement(
  1081. IN IAppHostElementCollection * pCollection,
  1082. OUT ENUM_INDEX * pIndex,
  1083. OUT IAppHostElement ** pElement
  1084. )
  1085. {
  1086. HRESULT hr = pCollection->get_Count(&pIndex->Count);
  1087. if (FAILED(hr))
  1088. {
  1089. DBGERROR_HR(hr);
  1090. return hr;
  1091. }
  1092. VariantInit(&pIndex->Index);
  1093. pIndex->Index.vt = VT_UI4;
  1094. pIndex->Index.ulVal = 0;
  1095. return FindNextElement(pCollection, pIndex, pElement);
  1096. }
  1097. HRESULT
  1098. FindNextElement(
  1099. IN IAppHostElementCollection * pCollection,
  1100. IN OUT ENUM_INDEX * pIndex,
  1101. OUT IAppHostElement ** pElement
  1102. )
  1103. {
  1104. *pElement = nullptr;
  1105. if (pIndex->Index.ulVal >= pIndex->Count)
  1106. {
  1107. return S_FALSE;
  1108. }
  1109. HRESULT hr = pCollection->get_Item(pIndex->Index, pElement);
  1110. if (SUCCEEDED(hr))
  1111. {
  1112. pIndex->Index.ulVal++;
  1113. }
  1114. return hr;
  1115. }
  1116. HRESULT
  1117. FindFirstChildElement(
  1118. IN IAppHostChildElementCollection * pCollection,
  1119. OUT ENUM_INDEX * pIndex,
  1120. OUT IAppHostElement ** pElement
  1121. )
  1122. {
  1123. HRESULT hr = pCollection->get_Count(&pIndex->Count);
  1124. if (FAILED(hr))
  1125. {
  1126. DBGERROR_HR(hr);
  1127. return hr;
  1128. }
  1129. VariantInit(&pIndex->Index);
  1130. pIndex->Index.vt = VT_UI4;
  1131. pIndex->Index.ulVal = 0;
  1132. return FindNextChildElement(pCollection, pIndex, pElement);
  1133. }
  1134. HRESULT
  1135. FindNextChildElement(
  1136. IN IAppHostChildElementCollection * pCollection,
  1137. IN OUT ENUM_INDEX * pIndex,
  1138. OUT IAppHostElement ** pElement
  1139. )
  1140. {
  1141. *pElement = nullptr;
  1142. if (pIndex->Index.ulVal >= pIndex->Count)
  1143. {
  1144. return S_FALSE;
  1145. }
  1146. HRESULT hr = pCollection->get_Item(pIndex->Index, pElement);
  1147. if (SUCCEEDED(hr))
  1148. {
  1149. pIndex->Index.ulVal++;
  1150. }
  1151. return hr;
  1152. }
  1153. HRESULT
  1154. FindFirstLocation(
  1155. IN IAppHostConfigLocationCollection * pCollection,
  1156. OUT ENUM_INDEX * pIndex,
  1157. OUT IAppHostConfigLocation ** pLocation
  1158. )
  1159. {
  1160. HRESULT hr = pCollection->get_Count(&pIndex->Count);
  1161. if (FAILED(hr))
  1162. {
  1163. DBGERROR_HR(hr);
  1164. return hr;
  1165. }
  1166. VariantInit(&pIndex->Index);
  1167. pIndex->Index.vt = VT_UI4;
  1168. pIndex->Index.ulVal = 0;
  1169. return FindNextLocation(pCollection, pIndex, pLocation);
  1170. }
  1171. HRESULT
  1172. FindNextLocation(
  1173. IN IAppHostConfigLocationCollection * pCollection,
  1174. IN OUT ENUM_INDEX * pIndex,
  1175. OUT IAppHostConfigLocation ** pLocation
  1176. )
  1177. {
  1178. *pLocation = nullptr;
  1179. if (pIndex->Index.ulVal >= pIndex->Count)
  1180. {
  1181. return S_FALSE;
  1182. }
  1183. HRESULT hr = pCollection->get_Item(pIndex->Index, pLocation);
  1184. if (SUCCEEDED(hr))
  1185. {
  1186. pIndex->Index.ulVal++;
  1187. }
  1188. return hr;
  1189. }
  1190. HRESULT
  1191. FindFirstLocationElement(
  1192. IN IAppHostConfigLocation * pLocation,
  1193. OUT ENUM_INDEX * pIndex,
  1194. OUT IAppHostElement ** pElement
  1195. )
  1196. {
  1197. HRESULT hr = pLocation->get_Count(&pIndex->Count);
  1198. if (FAILED(hr))
  1199. {
  1200. DBGERROR_HR(hr);
  1201. return hr;
  1202. }
  1203. VariantInit(&pIndex->Index);
  1204. pIndex->Index.vt = VT_UI4;
  1205. pIndex->Index.ulVal = 0;
  1206. return FindNextLocationElement(pLocation, pIndex, pElement);
  1207. }
  1208. HRESULT
  1209. FindNextLocationElement(
  1210. IN IAppHostConfigLocation * pLocation,
  1211. IN OUT ENUM_INDEX * pIndex,
  1212. OUT IAppHostElement ** pElement
  1213. )
  1214. {
  1215. *pElement = nullptr;
  1216. if (pIndex->Index.ulVal >= pIndex->Count)
  1217. {
  1218. return S_FALSE;
  1219. }
  1220. HRESULT hr = pLocation->get_Item(pIndex->Index, pElement);
  1221. if (SUCCEEDED(hr))
  1222. {
  1223. pIndex->Index.ulVal++;
  1224. }
  1225. return hr;
  1226. }
  1227. HRESULT
  1228. GetSharedConfigEnabled(
  1229. BOOL * pfIsSharedConfig
  1230. )
  1231. /*++
  1232. Routine Description:
  1233. Search the configuration for the shared configuration property.
  1234. Arguments:
  1235. pfIsSharedConfig - true if shared configuration is enabled
  1236. Return Value:
  1237. HRESULT
  1238. --*/
  1239. {
  1240. HRESULT hr = S_OK;
  1241. IAppHostAdminManager *pAdminManager = nullptr;
  1242. BSTR bstrConfigPath = nullptr;
  1243. IAppHostElement * pConfigRedirSection = nullptr;
  1244. BSTR bstrSectionName = SysAllocString(L"configurationRedirection");
  1245. if ( bstrSectionName == nullptr )
  1246. {
  1247. hr = E_OUTOFMEMORY;
  1248. DBGERROR_HR(hr);
  1249. goto exit;
  1250. }
  1251. bstrConfigPath = SysAllocString( L"MACHINE/REDIRECTION" );
  1252. if ( bstrConfigPath == nullptr )
  1253. {
  1254. hr = E_OUTOFMEMORY;
  1255. DBGERROR_HR(hr);
  1256. goto exit;
  1257. }
  1258. hr = CoCreateInstance( CLSID_AppHostAdminManager,
  1259. nullptr,
  1260. CLSCTX_INPROC_SERVER,
  1261. IID_IAppHostAdminManager,
  1262. (VOID **)&pAdminManager );
  1263. if( FAILED(hr) )
  1264. {
  1265. DBGERROR_HR(hr);
  1266. goto exit;
  1267. }
  1268. hr = pAdminManager->GetAdminSection( bstrSectionName,
  1269. bstrConfigPath,
  1270. &pConfigRedirSection );
  1271. if( FAILED(hr) )
  1272. {
  1273. DBGERROR_HR(hr);
  1274. goto exit;
  1275. }
  1276. hr = GetElementBoolProperty( pConfigRedirSection,
  1277. L"enabled",
  1278. pfIsSharedConfig );
  1279. if ( FAILED( hr ) )
  1280. {
  1281. DBGERROR_HR(hr);
  1282. goto exit;
  1283. }
  1284. pConfigRedirSection->Release();
  1285. pConfigRedirSection = nullptr;
  1286. exit:
  1287. //
  1288. // dump config exception to setup log file (if available)
  1289. //
  1290. if ( pConfigRedirSection != nullptr )
  1291. {
  1292. pConfigRedirSection->Release();
  1293. }
  1294. if ( pAdminManager != nullptr )
  1295. {
  1296. pAdminManager->Release();
  1297. }
  1298. if ( bstrConfigPath != nullptr )
  1299. {
  1300. SysFreeString( bstrConfigPath );
  1301. }
  1302. if ( bstrSectionName != nullptr )
  1303. {
  1304. SysFreeString( bstrSectionName );
  1305. }
  1306. return hr;
  1307. }