Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

768 lines
17 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) Microsoft Corporation, 1997 - 1999 all rights reserved.
  4. //
  5. // Module: sdohelperfuncs.cpp
  6. //
  7. // Project: Everest
  8. //
  9. // Description: Helper Functions
  10. //
  11. // Log:
  12. //
  13. // When Who What
  14. // ---- --- ----
  15. // 6/08/98 TLP Initial Version
  16. // 7/03/98 MAM Adapted from \ias\sdo\sdoias to use in UI
  17. // 11/03/98 MAM Moved GetSdo/PutSdo routines here from mmcutility.cpp
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "sdohelperfuncs.h"
  21. #include "comdef.h"
  22. //////////////////////////////////////////////////////////////////////////////
  23. // CORE HELPER FUNCTIONS
  24. //////////////////////////////////////////////////////////////////////////////
  25. ///////////////////////////////////////////////////////////////////
  26. HRESULT SDOGetCollectionEnumerator(
  27. ISdo* pSdo,
  28. LONG lPropertyId,
  29. IEnumVARIANT** ppEnum
  30. )
  31. {
  32. HRESULT hr;
  33. CComPtr<IUnknown> pUnknown;
  34. CComPtr<ISdoCollection> pSdoCollection;
  35. _variant_t vtDispatch;
  36. _ASSERT( NULL != pSdo && NULL == *ppEnum );
  37. hr = pSdo->GetProperty(lPropertyId, &vtDispatch);
  38. _ASSERT( VT_DISPATCH == V_VT(&vtDispatch) );
  39. if ( SUCCEEDED(hr) )
  40. {
  41. hr = vtDispatch.pdispVal->QueryInterface(IID_ISdoCollection, (void**)&pSdoCollection);
  42. if ( SUCCEEDED(hr) )
  43. {
  44. hr = pSdoCollection->get__NewEnum(&pUnknown);
  45. if ( SUCCEEDED(hr) )
  46. {
  47. hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)ppEnum);
  48. }
  49. }
  50. }
  51. return hr;
  52. }
  53. ///////////////////////////////////////////////////////////////////
  54. HRESULT SDONextObjectFromCollection(
  55. IEnumVARIANT* pEnum,
  56. ISdo** ppSdo
  57. )
  58. {
  59. HRESULT hr;
  60. DWORD dwRetrieved = 1;
  61. _variant_t vtDispatch;
  62. _ASSERT( NULL != pEnum && NULL == *ppSdo );
  63. hr = pEnum->Next(1, &vtDispatch, &dwRetrieved);
  64. _ASSERT( S_OK == hr || S_FALSE == hr );
  65. if ( S_OK == hr )
  66. {
  67. hr = vtDispatch.pdispVal->QueryInterface(IID_ISdo, (void**)ppSdo);
  68. }
  69. return hr;
  70. }
  71. ///////////////////////////////////////////////////////////////////
  72. HRESULT SDOGetComponentIdFromObject(
  73. ISdo* pSdo,
  74. LONG lPropertyId,
  75. PLONG pComponentId
  76. )
  77. {
  78. HRESULT hr;
  79. _variant_t vtProperty;
  80. _ASSERT( NULL != pSdo && NULL != pComponentId );
  81. hr = pSdo->GetProperty(lPropertyId, &vtProperty);
  82. if ( SUCCEEDED(hr) )
  83. {
  84. _ASSERT( VT_I4 == V_VT(&vtProperty) );
  85. *pComponentId = V_I4(&vtProperty);
  86. }
  87. return hr;
  88. }
  89. ///////////////////////////////////////////////////////////////////
  90. HRESULT SDOGetSdoFromCollection(
  91. ISdo* pSdoServer,
  92. LONG lCollectionPropertyId,
  93. LONG lComponentPropertyId,
  94. LONG lComponentId,
  95. ISdo** ppSdo
  96. )
  97. {
  98. HRESULT hr;
  99. CComPtr<IEnumVARIANT> pEnum;
  100. CComPtr<ISdo> pSdo;
  101. LONG ComponentId;
  102. do
  103. {
  104. hr = SDOGetCollectionEnumerator(
  105. pSdoServer,
  106. lCollectionPropertyId,
  107. &pEnum
  108. );
  109. if ( FAILED(hr) )
  110. break;
  111. hr = SDONextObjectFromCollection(pEnum,&pSdo);
  112. while( S_OK == hr )
  113. {
  114. hr = SDOGetComponentIdFromObject(
  115. pSdo,
  116. lComponentPropertyId,
  117. &ComponentId
  118. );
  119. if ( FAILED(hr) )
  120. break;
  121. if ( ComponentId == lComponentId )
  122. {
  123. pSdo.p->AddRef();
  124. *ppSdo = pSdo;
  125. break;
  126. }
  127. pSdo.Release();
  128. hr = SDONextObjectFromCollection(pEnum,&pSdo);
  129. }
  130. if ( S_OK != hr )
  131. hr = E_FAIL;
  132. } while ( FALSE );
  133. return hr;
  134. }
  135. //////////////////////////////////////////////////////////////////////////////
  136. /*++
  137. GetSdoVariant
  138. Gets a Variant from the SDO's and handles any error checking.
  139. --*/
  140. //////////////////////////////////////////////////////////////////////////////
  141. HRESULT GetSdoVariant(
  142. ISdo *pSdo
  143. , LONG lPropertyID
  144. , VARIANT * pVariant
  145. , UINT uiErrorID
  146. , HWND hWnd
  147. , IConsole *pConsole
  148. )
  149. {
  150. ATLTRACE(_T("# GetSdoVariant\n"));
  151. // Check for preconditions:
  152. _ASSERTE( pSdo != NULL );
  153. _ASSERTE( pVariant != NULL );
  154. HRESULT hr;
  155. hr = pSdo->GetProperty( lPropertyID, pVariant );
  156. if( FAILED( hr ) )
  157. {
  158. CComPtr<IUnknown> spUnknown(pSdo);
  159. if( spUnknown == NULL )
  160. {
  161. // Just put up an error dialog with the error string ID passed to us.
  162. ShowErrorDialog( hWnd, uiErrorID );
  163. }
  164. else
  165. {
  166. CComBSTR bstrError;
  167. HRESULT hrTemp = GetLastOLEErrorDescription( spUnknown, IID_ISdo, (BSTR *) &bstrError );
  168. if( SUCCEEDED( hr ) )
  169. {
  170. ShowErrorDialog( hWnd, 1, bstrError );
  171. }
  172. else
  173. {
  174. // Just put up an error dialog with the error string ID passed to us.
  175. ShowErrorDialog( hWnd, uiErrorID );
  176. }
  177. }
  178. }
  179. else
  180. {
  181. if( pVariant->vt == VT_EMPTY )
  182. {
  183. // This is not a real error -- we just need to
  184. // know that this item has not yet been initialized.
  185. return OLE_E_BLANK;
  186. }
  187. }
  188. return hr;
  189. }
  190. //////////////////////////////////////////////////////////////////////////////
  191. /*++
  192. GetSdoBSTR
  193. Gets a BSTR from the SDO's and handles any error checking.
  194. --*/
  195. //////////////////////////////////////////////////////////////////////////////
  196. HRESULT GetSdoBSTR(
  197. ISdo *pSdo
  198. , LONG lPropertyID
  199. , BSTR * pBSTR
  200. , UINT uiErrorID
  201. , HWND hWnd
  202. , IConsole *pConsole
  203. )
  204. {
  205. ATLTRACE(_T("# GetSdoBSTR\n"));
  206. // Check for preconditions:
  207. _ASSERTE( pSdo != NULL );
  208. _ASSERTE( pBSTR != NULL );
  209. HRESULT hr;
  210. CComVariant spVariant;
  211. hr = GetSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  212. if( SUCCEEDED( hr ) )
  213. {
  214. _ASSERTE( spVariant.vt == VT_BSTR );
  215. // Copy the string from the variant to the BSTR pointer passed in.
  216. if( SysReAllocString( pBSTR, spVariant.bstrVal ) == FALSE )
  217. {
  218. ShowErrorDialog( hWnd, uiErrorID );
  219. }
  220. }
  221. return hr;
  222. }
  223. //////////////////////////////////////////////////////////////////////////////
  224. /*++
  225. GetSdoBOOL
  226. Gets a BOOL from the SDO's and handles any error checking.
  227. --*/
  228. //////////////////////////////////////////////////////////////////////////////
  229. HRESULT GetSdoBOOL(
  230. ISdo *pSdo
  231. , LONG lPropertyID
  232. , BOOL * pBOOL
  233. , UINT uiErrorID
  234. , HWND hWnd
  235. , IConsole *pConsole
  236. )
  237. {
  238. ATLTRACE(_T("# GetSdoBOOL\n"));
  239. // Check for preconditions:
  240. _ASSERTE( pSdo != NULL );
  241. _ASSERTE( pBOOL != NULL );
  242. HRESULT hr;
  243. CComVariant spVariant;
  244. hr = GetSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  245. if( SUCCEEDED( hr ) )
  246. {
  247. _ASSERTE( spVariant.vt == VT_BOOL );
  248. // Copy the value from the variant to the BOOL pointer passed in.
  249. // We must do a quick and dirty conversion here because of the way OLE
  250. // does BOOL values.
  251. *pBOOL = ( spVariant.boolVal == VARIANT_TRUE ? TRUE : FALSE );
  252. }
  253. return hr;
  254. }
  255. //////////////////////////////////////////////////////////////////////////////
  256. /*++
  257. GetSdoI4
  258. Gets an I4 (LONG) from the SDO's and handles any error checking.
  259. --*/
  260. //////////////////////////////////////////////////////////////////////////////
  261. HRESULT GetSdoI4(
  262. ISdo *pSdo
  263. , LONG lPropertyID
  264. , LONG * pI4
  265. , UINT uiErrorID
  266. , HWND hWnd
  267. , IConsole *pConsole
  268. )
  269. {
  270. ATLTRACE(_T("# GetSdoI4\n"));
  271. // Check for preconditions:
  272. _ASSERTE( pSdo != NULL );
  273. _ASSERTE( pI4 != NULL );
  274. HRESULT hr;
  275. CComVariant spVariant;
  276. hr = GetSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  277. if( SUCCEEDED( hr ) )
  278. {
  279. _ASSERTE( spVariant.vt == VT_I4 );
  280. // Copy the value from the variant to the BOOL pointer passed in.
  281. *pI4 = spVariant.lVal;
  282. }
  283. return hr;
  284. }
  285. //////////////////////////////////////////////////////////////////////////////
  286. /*++
  287. PutSdoVariant
  288. Writes a Variant to the SDO's and handles any error checking.
  289. --*/
  290. //////////////////////////////////////////////////////////////////////////////
  291. HRESULT PutSdoVariant(
  292. ISdo *pSdo
  293. , LONG lPropertyID
  294. , VARIANT * pVariant
  295. , UINT uiErrorID
  296. , HWND hWnd
  297. , IConsole *pConsole
  298. )
  299. {
  300. ATLTRACE(_T("# PutSdoVariant\n"));
  301. // Check for preconditions:
  302. _ASSERTE( pSdo != NULL );
  303. _ASSERTE( pVariant != NULL );
  304. HRESULT hr;
  305. hr = pSdo->PutProperty( lPropertyID, pVariant );
  306. if( FAILED( hr ) )
  307. {
  308. CComPtr<IUnknown> spUnknown(pSdo);
  309. if( spUnknown == NULL )
  310. {
  311. // Just put up an error dialog with the error string ID passed to us.
  312. ShowErrorDialog(
  313. hWnd,
  314. uiErrorID,
  315. NULL,
  316. S_OK,
  317. USE_DEFAULT,
  318. pConsole,
  319. MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL
  320. );
  321. }
  322. else
  323. {
  324. CComBSTR bstrError;
  325. HRESULT hrTemp = GetLastOLEErrorDescription( spUnknown, IID_ISdo, (BSTR *) &bstrError );
  326. if( SUCCEEDED( hr ) )
  327. {
  328. ShowErrorDialog(
  329. hWnd,
  330. 1,
  331. bstrError,
  332. S_OK,
  333. USE_DEFAULT,
  334. pConsole,
  335. MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL
  336. );
  337. }
  338. else
  339. {
  340. // Just put up an error dialog with the error string ID passed to us.
  341. ShowErrorDialog(
  342. hWnd,
  343. uiErrorID,
  344. NULL,
  345. S_OK,
  346. USE_DEFAULT,
  347. pConsole,
  348. MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL
  349. );
  350. }
  351. }
  352. }
  353. return hr;
  354. }
  355. //////////////////////////////////////////////////////////////////////////////
  356. /*++
  357. PutSdoBSTR
  358. Writes a BSTR to the SDO's and handles any error checking.
  359. --*/
  360. //////////////////////////////////////////////////////////////////////////////
  361. HRESULT PutSdoBSTR(
  362. ISdo *pSdo
  363. , LONG lPropertyID
  364. , BSTR *pBSTR
  365. , UINT uiErrorID
  366. , HWND hWnd
  367. , IConsole *pConsole
  368. )
  369. {
  370. ATLTRACE(_T("# PutSdoBSTR\n"));
  371. // Check for preconditions:
  372. _ASSERTE( pSdo != NULL );
  373. _ASSERTE( pBSTR != NULL );
  374. HRESULT hr;
  375. CComVariant spVariant;
  376. // Load the Variant with the required info.
  377. spVariant.vt = VT_BSTR;
  378. spVariant.bstrVal = SysAllocString( *pBSTR );
  379. if( spVariant.bstrVal == NULL )
  380. {
  381. ShowErrorDialog( hWnd, uiErrorID );
  382. }
  383. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  384. return hr;
  385. }
  386. //////////////////////////////////////////////////////////////////////////////
  387. /*++
  388. PutSdoBOOL
  389. Writes a BOOL to the SDO's and handles any error checking.
  390. --*/
  391. //////////////////////////////////////////////////////////////////////////////
  392. HRESULT PutSdoBOOL(
  393. ISdo *pSdo
  394. , LONG lPropertyID
  395. , BOOL bValue
  396. , UINT uiErrorID
  397. , HWND hWnd
  398. , IConsole *pConsole
  399. )
  400. {
  401. ATLTRACE(_T("# PutSdoBOOL\n"));
  402. // Check for preconditions:
  403. _ASSERTE( pSdo != NULL );
  404. HRESULT hr;
  405. CComVariant spVariant;
  406. // Load the Variant with the required info.
  407. // We have to do a little mapping here because of the way Automation does BOOL's.
  408. spVariant.vt = VT_BOOL;
  409. spVariant.boolVal = ( bValue ? VARIANT_TRUE : VARIANT_FALSE );
  410. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  411. return hr;
  412. }
  413. //////////////////////////////////////////////////////////////////////////////
  414. /*++
  415. PutSdoI4
  416. Writes an I4 (LONG) to the SDO's and handles any error checking.
  417. --*/
  418. //////////////////////////////////////////////////////////////////////////////
  419. HRESULT PutSdoI4(
  420. ISdo *pSdo
  421. , LONG lPropertyID
  422. , LONG lValue
  423. , UINT uiErrorID
  424. , HWND hWnd
  425. , IConsole *pConsole
  426. )
  427. {
  428. ATLTRACE(_T("# PutSdoI4\n"));
  429. // Check for preconditions:
  430. _ASSERTE( pSdo != NULL );
  431. HRESULT hr;
  432. CComVariant spVariant;
  433. // Load the Variant with the required info.
  434. spVariant.vt = VT_I4;
  435. spVariant.lVal = lValue;
  436. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  437. return hr;
  438. }
  439. //////////////////////////////////////////////////////////////////////////////
  440. /*++
  441. GetLastOLEErrorDescription
  442. Gets an error string from an interface.
  443. --*/
  444. //////////////////////////////////////////////////////////////////////////////
  445. HRESULT GetLastOLEErrorDescription(
  446. IUnknown *pUnknown
  447. , REFIID riid
  448. , BSTR *pbstrError
  449. )
  450. {
  451. ATLTRACE(_T("# GetLastOLEErrorDescription\n"));
  452. // Check for preconditions:
  453. _ASSERTE( pUnknown != NULL );
  454. HRESULT hr;
  455. CComQIPtr<ISupportErrorInfo, &IID_ISupportErrorInfo> spSupportErrorInfo(pUnknown);
  456. if( spSupportErrorInfo == NULL )
  457. {
  458. return E_NOINTERFACE;
  459. }
  460. hr = spSupportErrorInfo->InterfaceSupportsErrorInfo(riid);
  461. if( S_OK != hr )
  462. {
  463. return E_FAIL;
  464. }
  465. CComPtr<IErrorInfo> spErrorInfo;
  466. hr = GetErrorInfo( /* reserved */ 0, (IErrorInfo **) &spErrorInfo );
  467. if( hr != S_OK || spErrorInfo == NULL )
  468. {
  469. return E_FAIL;
  470. }
  471. hr = spErrorInfo->GetDescription( pbstrError );
  472. return hr;
  473. }
  474. //////////////////////////////////////////////////////////////////////////////
  475. /*++
  476. VendorsVector::VendorsVector
  477. Constructor for STL vector wrapper for the SDO's vendor list.
  478. Can throw E_FAIL or exceptions from std::vector::push_back.
  479. --*/
  480. //////////////////////////////////////////////////////////////////////////////
  481. VendorsVector::VendorsVector( ISdoCollection * pSdoVendors )
  482. {
  483. HRESULT hr = S_OK;
  484. CComPtr<IUnknown> spUnknown;
  485. CComPtr<IEnumVARIANT> spEnumVariant;
  486. CComVariant spVariant;
  487. long ulCount;
  488. ULONG ulCountReceived;
  489. if( ! pSdoVendors )
  490. {
  491. throw E_FAIL; // Is there a better error to return here?
  492. }
  493. // We check the count of items in our collection and don't bother getting the
  494. // enumerator if the count is zero.
  495. // This saves time and also helps us to avoid a bug in the the enumerator which
  496. // causes it to fail if we call next when it is empty.
  497. pSdoVendors->get_Count( & ulCount );
  498. if( ulCount > 0 )
  499. {
  500. // Get the enumerator for the Clients collection.
  501. hr = pSdoVendors->get__NewEnum( (IUnknown **) & spUnknown );
  502. if( FAILED( hr ) || ! spUnknown )
  503. {
  504. throw E_FAIL;
  505. }
  506. hr = spUnknown->QueryInterface( IID_IEnumVARIANT, (void **) &spEnumVariant );
  507. spUnknown.Release();
  508. if( FAILED( hr ) || ! spEnumVariant )
  509. {
  510. throw E_FAIL;
  511. }
  512. // Get the first item.
  513. hr = spEnumVariant->Next( 1, & spVariant, &ulCountReceived );
  514. while( SUCCEEDED( hr ) && ulCountReceived == 1 )
  515. {
  516. // Get an sdo pointer from the variant we received.
  517. if( spVariant.vt != VT_DISPATCH || ! spVariant.pdispVal )
  518. {
  519. _ASSERTE( FALSE );
  520. continue;
  521. }
  522. CComPtr<ISdo> spSdo;
  523. hr = spVariant.pdispVal->QueryInterface( IID_ISdo, (void **) &spSdo );
  524. spVariant.Clear();
  525. if( FAILED( hr ) )
  526. {
  527. _ASSERTE( FALSE );
  528. continue;
  529. }
  530. // Get Vendor Name.
  531. hr = spSdo->GetProperty( PROPERTY_SDO_NAME, &spVariant );
  532. if( FAILED( hr ) )
  533. {
  534. _ASSERTE( FALSE );
  535. continue;
  536. }
  537. _ASSERTE( spVariant.vt == VT_BSTR );
  538. CComBSTR bstrVendorName = spVariant.bstrVal;
  539. spVariant.Clear();
  540. // Get Vendor ID.
  541. hr = spSdo->GetProperty( PROPERTY_NAS_VENDOR_ID, &spVariant );
  542. if( FAILED( hr ) )
  543. {
  544. _ASSERTE( FALSE );
  545. continue;
  546. }
  547. _ASSERTE( spVariant.vt == VT_I4 );
  548. LONG lVendorID = spVariant.lVal;
  549. spVariant.Clear();
  550. // Add the vendor infor to the list of vendors.
  551. push_back( std::make_pair(bstrVendorName, lVendorID) );
  552. // Get the next item.
  553. hr = spEnumVariant->Next( 1, & spVariant, &ulCountReceived );
  554. }
  555. }
  556. else
  557. {
  558. // There are no items in the enumeration
  559. // Do nothing.
  560. }
  561. }
  562. //////////////////////////////////////////////////////////////////////////////
  563. /*++
  564. VendorsVector::VendorIDToOrdinal
  565. Given a RADIUS vendor ID, tells you the position of that vendor in the vector.
  566. --*/
  567. //////////////////////////////////////////////////////////////////////////////
  568. int VendorsVector::VendorIDToOrdinal( LONG lVendorID )
  569. {
  570. for (int i = 0; i < size() ; ++i)
  571. {
  572. if( lVendorID == operator[](i).second )
  573. {
  574. return i;
  575. }
  576. }
  577. return 0;
  578. }