Source code of Windows XP (NT5)
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.

745 lines
15 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->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( hWnd, uiErrorID );
  313. }
  314. else
  315. {
  316. CComBSTR bstrError;
  317. HRESULT hrTemp = GetLastOLEErrorDescription( spUnknown, IID_ISdo, (BSTR *) &bstrError );
  318. if( SUCCEEDED( hr ) )
  319. {
  320. ShowErrorDialog( hWnd, 1, bstrError );
  321. }
  322. else
  323. {
  324. // Just put up an error dialog with the error string ID passed to us.
  325. ShowErrorDialog( hWnd, uiErrorID );
  326. }
  327. }
  328. }
  329. return hr;
  330. }
  331. //////////////////////////////////////////////////////////////////////////////
  332. /*++
  333. PutSdoBSTR
  334. Writes a BSTR to the SDO's and handles any error checking.
  335. --*/
  336. //////////////////////////////////////////////////////////////////////////////
  337. HRESULT PutSdoBSTR(
  338. ISdo *pSdo
  339. , LONG lPropertyID
  340. , BSTR *pBSTR
  341. , UINT uiErrorID
  342. , HWND hWnd
  343. , IConsole *pConsole
  344. )
  345. {
  346. ATLTRACE(_T("# PutSdoBSTR\n"));
  347. // Check for preconditions:
  348. _ASSERTE( pSdo != NULL );
  349. _ASSERTE( pBSTR != NULL );
  350. HRESULT hr;
  351. CComVariant spVariant;
  352. // Load the Variant with the required info.
  353. spVariant.vt = VT_BSTR;
  354. spVariant.bstrVal = SysAllocString( *pBSTR );
  355. if( spVariant.bstrVal == NULL )
  356. {
  357. ShowErrorDialog( hWnd, uiErrorID );
  358. }
  359. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  360. return hr;
  361. }
  362. //////////////////////////////////////////////////////////////////////////////
  363. /*++
  364. PutSdoBOOL
  365. Writes a BOOL to the SDO's and handles any error checking.
  366. --*/
  367. //////////////////////////////////////////////////////////////////////////////
  368. HRESULT PutSdoBOOL(
  369. ISdo *pSdo
  370. , LONG lPropertyID
  371. , BOOL bValue
  372. , UINT uiErrorID
  373. , HWND hWnd
  374. , IConsole *pConsole
  375. )
  376. {
  377. ATLTRACE(_T("# PutSdoBOOL\n"));
  378. // Check for preconditions:
  379. _ASSERTE( pSdo != NULL );
  380. HRESULT hr;
  381. CComVariant spVariant;
  382. // Load the Variant with the required info.
  383. // We have to do a little mapping here because of the way Automation does BOOL's.
  384. spVariant.vt = VT_BOOL;
  385. spVariant.boolVal = ( bValue ? VARIANT_TRUE : VARIANT_FALSE );
  386. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  387. return hr;
  388. }
  389. //////////////////////////////////////////////////////////////////////////////
  390. /*++
  391. PutSdoI4
  392. Writes an I4 (LONG) to the SDO's and handles any error checking.
  393. --*/
  394. //////////////////////////////////////////////////////////////////////////////
  395. HRESULT PutSdoI4(
  396. ISdo *pSdo
  397. , LONG lPropertyID
  398. , LONG lValue
  399. , UINT uiErrorID
  400. , HWND hWnd
  401. , IConsole *pConsole
  402. )
  403. {
  404. ATLTRACE(_T("# PutSdoI4\n"));
  405. // Check for preconditions:
  406. _ASSERTE( pSdo != NULL );
  407. HRESULT hr;
  408. CComVariant spVariant;
  409. // Load the Variant with the required info.
  410. spVariant.vt = VT_I4;
  411. spVariant.lVal = lValue;
  412. hr = PutSdoVariant( pSdo, lPropertyID, &spVariant, uiErrorID, hWnd, pConsole );
  413. return hr;
  414. }
  415. //////////////////////////////////////////////////////////////////////////////
  416. /*++
  417. GetLastOLEErrorDescription
  418. Gets an error string from an interface.
  419. --*/
  420. //////////////////////////////////////////////////////////////////////////////
  421. HRESULT GetLastOLEErrorDescription(
  422. IUnknown *pUnknown
  423. , REFIID riid
  424. , BSTR *pbstrError
  425. )
  426. {
  427. ATLTRACE(_T("# GetLastOLEErrorDescription\n"));
  428. // Check for preconditions:
  429. _ASSERTE( pUnknown != NULL );
  430. HRESULT hr;
  431. CComQIPtr<ISupportErrorInfo, &IID_ISupportErrorInfo> spSupportErrorInfo(pUnknown);
  432. if( spSupportErrorInfo == NULL )
  433. {
  434. return E_NOINTERFACE;
  435. }
  436. hr = spSupportErrorInfo->InterfaceSupportsErrorInfo(riid);
  437. if( S_OK != hr )
  438. {
  439. return E_FAIL;
  440. }
  441. CComPtr<IErrorInfo> spErrorInfo;
  442. hr = GetErrorInfo( /* reserved */ 0, (IErrorInfo **) &spErrorInfo );
  443. if( hr != S_OK || spErrorInfo == NULL )
  444. {
  445. return E_FAIL;
  446. }
  447. hr = spErrorInfo->GetDescription( pbstrError );
  448. return hr;
  449. }
  450. //////////////////////////////////////////////////////////////////////////////
  451. /*++
  452. VendorsVector::VendorsVector
  453. Constructor for STL vector wrapper for the SDO's vendor list.
  454. Can throw E_FAIL or exceptions from std::vector::push_back.
  455. --*/
  456. //////////////////////////////////////////////////////////////////////////////
  457. VendorsVector::VendorsVector( ISdoCollection * pSdoVendors )
  458. {
  459. HRESULT hr = S_OK;
  460. CComPtr<IUnknown> spUnknown;
  461. CComPtr<IEnumVARIANT> spEnumVariant;
  462. CComVariant spVariant;
  463. long ulCount;
  464. ULONG ulCountReceived;
  465. if( ! pSdoVendors )
  466. {
  467. throw E_FAIL; // Is there a better error to return here?
  468. }
  469. // We check the count of items in our collection and don't bother getting the
  470. // enumerator if the count is zero.
  471. // This saves time and also helps us to avoid a bug in the the enumerator which
  472. // causes it to fail if we call next when it is empty.
  473. pSdoVendors->get_Count( & ulCount );
  474. if( ulCount > 0 )
  475. {
  476. // Get the enumerator for the Clients collection.
  477. hr = pSdoVendors->get__NewEnum( (IUnknown **) & spUnknown );
  478. if( FAILED( hr ) || ! spUnknown )
  479. {
  480. throw E_FAIL;
  481. }
  482. hr = spUnknown->QueryInterface( IID_IEnumVARIANT, (void **) &spEnumVariant );
  483. spUnknown.Release();
  484. if( FAILED( hr ) || ! spEnumVariant )
  485. {
  486. throw E_FAIL;
  487. }
  488. // Get the first item.
  489. hr = spEnumVariant->Next( 1, & spVariant, &ulCountReceived );
  490. while( SUCCEEDED( hr ) && ulCountReceived == 1 )
  491. {
  492. // Get an sdo pointer from the variant we received.
  493. if( spVariant.vt != VT_DISPATCH || ! spVariant.pdispVal )
  494. {
  495. _ASSERTE( FALSE );
  496. continue;
  497. }
  498. CComPtr<ISdo> spSdo;
  499. hr = spVariant.pdispVal->QueryInterface( IID_ISdo, (void **) &spSdo );
  500. spVariant.Clear();
  501. if( FAILED( hr ) )
  502. {
  503. _ASSERTE( FALSE );
  504. continue;
  505. }
  506. // Get Vendor Name.
  507. hr = spSdo->GetProperty( PROPERTY_SDO_NAME, &spVariant );
  508. if( FAILED( hr ) )
  509. {
  510. _ASSERTE( FALSE );
  511. continue;
  512. }
  513. _ASSERTE( spVariant.vt == VT_BSTR );
  514. CComBSTR bstrVendorName = spVariant.bstrVal;
  515. spVariant.Clear();
  516. // Get Vendor ID.
  517. hr = spSdo->GetProperty( PROPERTY_NAS_VENDOR_ID, &spVariant );
  518. if( FAILED( hr ) )
  519. {
  520. _ASSERTE( FALSE );
  521. continue;
  522. }
  523. _ASSERTE( spVariant.vt == VT_I4 );
  524. LONG lVendorID = spVariant.lVal;
  525. spVariant.Clear();
  526. // Add the vendor infor to the list of vendors.
  527. push_back( std::make_pair(bstrVendorName, lVendorID) );
  528. // Get the next item.
  529. hr = spEnumVariant->Next( 1, & spVariant, &ulCountReceived );
  530. }
  531. }
  532. else
  533. {
  534. // There are no items in the enumeration
  535. // Do nothing.
  536. }
  537. }
  538. //////////////////////////////////////////////////////////////////////////////
  539. /*++
  540. VendorsVector::VendorIDToOrdinal
  541. Given a RADIUS vendor ID, tells you the position of that vendor in the vector.
  542. --*/
  543. //////////////////////////////////////////////////////////////////////////////
  544. int VendorsVector::VendorIDToOrdinal( LONG lVendorID )
  545. {
  546. for (int i = 0; i < size() ; ++i)
  547. {
  548. if( lVendorID == operator[](i).second )
  549. {
  550. return i;
  551. }
  552. }
  553. return 0;
  554. }