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.

1255 lines
33 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: ResCtrl.cpp
  4. //
  5. // Synopsis: This file holds the implementation of the
  6. // of CResCtrl class
  7. //
  8. // History: 01/15/2001 serdarun Created
  9. //
  10. // Copyright (C) 2000-2001 Microsoft Corporation
  11. // All rights reserved.
  12. //
  13. //#--------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "Localuiresource.h"
  16. #include "ResCtrl.h"
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CResCtrl
  19. //
  20. // currently supports at most 7 resources due LCD size
  21. //
  22. #define MAX_RESOURCE_COUNT 7
  23. const WCHAR ELEMENT_RETRIEVER [] = L"Elementmgr.ElementRetriever";
  24. const WCHAR RESOURCE_CONTAINER [] = L"LocalUIResource";
  25. //
  26. // localui resource definition properties
  27. //
  28. const WCHAR CAPTION_PROPERTY [] = L"CaptionRID";
  29. const WCHAR SOURCE_PROPERTY [] = L"Source";
  30. const WCHAR RESOURCENAME_PROPERTY []= L"ResourceName";
  31. const WCHAR MERIT_PROPERTY [] = L"Merit";
  32. const WCHAR STATE_PROPERTY [] = L"State";
  33. const WCHAR TEXTRESOURCE_PROPERTY [] = L"IsTextResource";
  34. const WCHAR UNIQUE_NAME [] = L"UniqueName";
  35. const WCHAR DISPLAY_INFORMATION [] = L"DisplayInformationID";
  36. //
  37. // registry path where the resource information is stored
  38. //
  39. const WCHAR RESOURCE_REGISTRY_PATH [] =
  40. L"SOFTWARE\\Microsoft\\ServerAppliance\\LocalizationManager\\Resources";
  41. //
  42. // language ID value
  43. //
  44. const WCHAR LANGID_VALUE [] = L"LANGID";
  45. //
  46. // resource directory
  47. //
  48. const WCHAR RESOURCE_DIRECTORY [] = L"ResourceDirectory";
  49. const WCHAR DEFAULT_DIRECTORY [] =
  50. L"%systemroot%\\system32\\ServerAppliance\\mui";
  51. const WCHAR DEFAULT_EXPANDED_DIRECTORY [] =
  52. L"C:\\winnt\\system32\\ServerAppliance\\mui";
  53. const WCHAR DELIMITER [] = L"\\";
  54. const WCHAR NEW_LANGID_VALUE [] = L"NewLANGID";
  55. const WCHAR DEFAULT_LANGID[] = L"0409";
  56. //++--------------------------------------------------------------
  57. //
  58. // Function: AddIconResource
  59. //
  60. // Synopsis: This is the CResCtrl method to retrieve
  61. // each resource information
  62. //
  63. // Arguments: IWebElement * pElement
  64. //
  65. // Returns: HRESULT - success/failure
  66. //
  67. // History: serdarun Created 01/01/2001
  68. //
  69. //----------------------------------------------------------------
  70. STDMETHODIMP CResCtrl::AddIconResource(IWebElement * pElement)
  71. {
  72. HRESULT hr;
  73. USES_CONVERSION;
  74. CComVariant varUniqueName;
  75. wstring wsUniqueName;
  76. wstring wsSource;
  77. wstring wsIcon;
  78. wstring wsIconKey;
  79. DWORD dwMerit;
  80. ResourceStructPtr pResourceStruct = NULL;
  81. CComBSTR bstrResourceName = CComBSTR(RESOURCENAME_PROPERTY);
  82. CComBSTR bstrSourceProp = CComBSTR(SOURCE_PROPERTY);
  83. CComBSTR bstrMeritProp = CComBSTR(MERIT_PROPERTY);
  84. CComBSTR bstrCaptionProp = CComBSTR(CAPTION_PROPERTY);
  85. if ( (bstrResourceName.m_str == NULL) ||
  86. (bstrSourceProp.m_str == NULL) ||
  87. (bstrMeritProp.m_str == NULL) ||
  88. (bstrCaptionProp.m_str == NULL) )
  89. {
  90. SATraceString("CResCtrl::AddIconResource failed on memory allocation ");
  91. return E_OUTOFMEMORY;
  92. }
  93. //
  94. // get the unique name for the resource
  95. //
  96. hr = pElement->GetProperty (bstrResourceName, &varUniqueName);
  97. if (FAILED(hr))
  98. {
  99. SATraceString ("CResCtrl::AddIconResource failed on getting uniquename");
  100. return hr;
  101. }
  102. //
  103. // store the unique name for later use
  104. //
  105. wsUniqueName = V_BSTR (&varUniqueName);
  106. //
  107. // get the resource dll for the resource
  108. //
  109. CComVariant varSource;
  110. hr = pElement->GetProperty (bstrSourceProp, &varSource);
  111. if (FAILED(hr))
  112. {
  113. SATraceString ("CResCtrl::AddIconResource failed on getting resource dll");
  114. return hr;
  115. }
  116. wsSource.assign(m_wstrResourceDir);
  117. wsSource.append(V_BSTR (&varSource));
  118. //
  119. // load the resource dll
  120. //
  121. HINSTANCE hInstance = NULL;
  122. hInstance = LoadLibrary(wsSource.c_str());
  123. if (NULL == hInstance)
  124. {
  125. DWORD dwError = GetLastError();
  126. SATracePrintf ("CResCtrl::AddIconResource failed on LoadLibrary:%x",dwError);
  127. return E_FAIL;
  128. }
  129. //
  130. // allocate a new struct for the resource
  131. //
  132. pResourceStruct = new ResourceStruct;
  133. if (NULL == pResourceStruct)
  134. {
  135. FreeLibrary(hInstance);
  136. return E_OUTOFMEMORY;
  137. }
  138. //
  139. // set default values
  140. //
  141. pResourceStruct->lState = 0;
  142. //
  143. // get the merit for resource
  144. //
  145. CComVariant varResMerit;
  146. hr = pElement->GetProperty (bstrMeritProp, &varResMerit);
  147. if (FAILED(hr))
  148. {
  149. SATraceString ("CResCtrl::AddIconResource failed on getting merit");
  150. FreeLibrary(hInstance);
  151. return hr;
  152. }
  153. dwMerit = V_I4 (&varResMerit);
  154. //
  155. // get the default icon resource id
  156. //
  157. CComVariant varResIcon;
  158. hr = pElement->GetProperty (bstrCaptionProp, &varResIcon);
  159. if (FAILED(hr))
  160. {
  161. SATraceString ("CResCtrl::AddIconResource failed on getting captionrid");
  162. FreeLibrary(hInstance);
  163. return hr;
  164. }
  165. int iCount = 0;
  166. //
  167. // icon resource string
  168. //
  169. wsIcon = V_BSTR (&varResIcon);
  170. //
  171. // while there are state icons
  172. //
  173. while (SUCCEEDED(hr))
  174. {
  175. HANDLE hIcon = NULL;
  176. //
  177. // load the icon from resource dll
  178. //
  179. hIcon = ::LoadImage (
  180. hInstance,
  181. MAKEINTRESOURCE(HexStringToULong(wsIcon)),
  182. IMAGE_ICON,
  183. 16,
  184. 16,
  185. LR_MONOCHROME
  186. );
  187. if (NULL == hIcon)
  188. {
  189. SATraceString ("Loading the icon failed, continue...");
  190. }
  191. //
  192. // insert the icon to state-icon map
  193. //
  194. (pResourceStruct->mapResIcon).insert(ResourceIconMap::value_type(iCount,(HICON)hIcon));
  195. //
  196. // create statekey, state0, state1...
  197. //
  198. iCount++;
  199. WCHAR wstrCount[10];
  200. _itow(iCount,wstrCount,10);
  201. wsIconKey = L"State";
  202. wsIconKey.append(wstring(wstrCount));
  203. CComBSTR bstrIconKey = CComBSTR(wsIconKey.c_str());
  204. if (bstrIconKey.m_str == NULL)
  205. {
  206. SATraceString(" CTextResCtrl::AddTextResource failed on memory allocation ");
  207. return E_OUTOFMEMORY;
  208. }
  209. varResIcon.Clear();
  210. //
  211. // get the resource id for state icon
  212. //
  213. hr = pElement->GetProperty (bstrIconKey, &varResIcon);
  214. if (SUCCEEDED(hr))
  215. {
  216. wsIcon = V_BSTR (&varResIcon);
  217. }
  218. }
  219. //
  220. // increment the number of resources
  221. //
  222. m_lResourceCount++;
  223. //
  224. // insert the info to the resource map
  225. //
  226. m_ResourceMap.insert(ResourceMap::value_type(wsUniqueName,pResourceStruct));
  227. //
  228. // insert merit and resource name to merit map
  229. //
  230. m_MeritMap.insert(MeritMap::value_type(dwMerit,wsUniqueName));
  231. FreeLibrary(hInstance);
  232. return S_OK;
  233. }// end of CResCtrl::AddIconResource
  234. //++--------------------------------------------------------------
  235. //
  236. // Function: GetLocalUIResources
  237. //
  238. // Synopsis: This is the CResCtrl method to retrieve
  239. // each resource from element manager
  240. //
  241. // Arguments: none
  242. //
  243. // Returns: HRESULT - success/failure
  244. //
  245. // History: serdarun Created 01/01/2001
  246. //
  247. //----------------------------------------------------------------
  248. STDMETHODIMP CResCtrl::GetLocalUIResources()
  249. {
  250. HRESULT hr;
  251. CLSID clsid;
  252. CComPtr<IWebElementRetriever> pWebElementRetriever = NULL;
  253. CComPtr<IDispatch> pDispatch = NULL;
  254. CComPtr<IWebElementEnum> pWebElementEnum = NULL;
  255. CComPtr<IUnknown> pUnknown = NULL;
  256. CComPtr<IEnumVARIANT> pEnumVariant = NULL;
  257. CComVariant varElement;
  258. DWORD dwElementsLeft = 0;
  259. CComBSTR bstrTextResource = CComBSTR(TEXTRESOURCE_PROPERTY);
  260. CComBSTR bstrResourceContainer = CComBSTR(RESOURCE_CONTAINER);
  261. if ( (bstrTextResource.m_str == NULL) ||
  262. (bstrResourceContainer.m_str == NULL) )
  263. {
  264. SATraceString(" CResCtrl::GetLocalUIResources failed on memory allocation ");
  265. return E_OUTOFMEMORY;
  266. }
  267. //
  268. // get the resource directory
  269. //
  270. hr = GetResourceDirectory(m_wstrResourceDir);
  271. if (FAILED (hr))
  272. {
  273. SATracePrintf ("CResCtrl::GetLocalUIResources failed on GetResourceDirectory:%x",hr);
  274. return hr;
  275. }
  276. //
  277. // get the CLSID for Element manager
  278. //
  279. hr = ::CLSIDFromProgID (ELEMENT_RETRIEVER,&clsid);
  280. if (FAILED (hr))
  281. {
  282. SATracePrintf ("CResCtrl::GetLocalUIResources failed on CLSIDFromProgID:%x",hr);
  283. return hr;
  284. }
  285. //
  286. // create the Element Retriever now
  287. //
  288. hr = ::CoCreateInstance (
  289. clsid,
  290. NULL,
  291. CLSCTX_LOCAL_SERVER,
  292. IID_IWebElementRetriever,
  293. (PVOID*) &pWebElementRetriever
  294. );
  295. if (FAILED (hr))
  296. {
  297. SATracePrintf ("CResCtrl::GetLocalUIResources failed on CoCreateInstance:%x",hr);
  298. return hr;
  299. }
  300. //
  301. // get localui resource elements
  302. //
  303. hr = pWebElementRetriever->GetElements (
  304. 1,
  305. bstrResourceContainer,
  306. &pDispatch
  307. );
  308. if (FAILED (hr))
  309. {
  310. SATracePrintf ("CResCtrl::GetLocalUIResources failed on GetElements:%x",hr);
  311. return hr;
  312. }
  313. //
  314. // get the enum variant
  315. //
  316. hr = pDispatch->QueryInterface (
  317. IID_IWebElementEnum,
  318. (LPVOID*) (&pWebElementEnum)
  319. );
  320. if (FAILED (hr))
  321. {
  322. SATracePrintf ("CResCtrl::GetLocalUIResources failed on QueryInterface:%x",hr);
  323. return hr;
  324. }
  325. m_lResourceCount = 0;
  326. //
  327. // get number of resource elements
  328. //
  329. hr = pWebElementEnum->get_Count (&m_lResourceCount);
  330. if (FAILED (hr))
  331. {
  332. SATracePrintf ("CResCtrl::GetLocalUIResources failed on get_Count:%x",hr);
  333. return hr;
  334. }
  335. SATracePrintf ("CResCtrl::GetLocalUIResources failed on QueryInterface:%d",m_lResourceCount);
  336. //
  337. // no resources, just return
  338. //
  339. if (0 == m_lResourceCount)
  340. {
  341. return S_FALSE;
  342. }
  343. hr = pWebElementEnum->get__NewEnum (&pUnknown);
  344. if (FAILED (hr))
  345. {
  346. SATracePrintf ("CResCtrl::GetLocalUIResources failed on get__NewEnum:%x",hr);
  347. return hr;
  348. }
  349. //
  350. // get the enum variant
  351. //
  352. hr = pUnknown->QueryInterface (
  353. IID_IEnumVARIANT,
  354. (LPVOID*)(&pEnumVariant)
  355. );
  356. if (FAILED (hr))
  357. {
  358. SATracePrintf ("CResCtrl::GetLocalUIResources failed on QueryInterface:%x",hr);
  359. return hr;
  360. }
  361. //
  362. // get elements out of the collection and initialize
  363. //
  364. hr = pEnumVariant->Next (1, &varElement, &dwElementsLeft);
  365. if (FAILED (hr))
  366. {
  367. SATracePrintf ("CResCtrl::GetLocalUIResources failed on Next:%x",hr);
  368. }
  369. m_lResourceCount = 0;
  370. //
  371. // for each resource
  372. //
  373. while ((dwElementsLeft> 0) && (SUCCEEDED (hr)) && (m_lResourceCount<MAX_RESOURCE_COUNT))
  374. {
  375. //
  376. // get the IWebElement Interface
  377. //
  378. CComPtr <IWebElement> pElement;
  379. hr = varElement.pdispVal->QueryInterface (
  380. __uuidof (IWebElement),
  381. (LPVOID*)(&pElement)
  382. );
  383. if (FAILED (hr))
  384. {
  385. SATracePrintf ("CResCtrl::GetLocalUIResources failed on QueryInterface:%x",hr);
  386. }
  387. //
  388. // check if it is a text resource
  389. //
  390. CComVariant varIsTextResource;
  391. hr = pElement->GetProperty (bstrTextResource, &varIsTextResource);
  392. if (SUCCEEDED(hr))
  393. {
  394. if (0 == V_I4(&varIsTextResource))
  395. {
  396. AddIconResource(pElement);
  397. }
  398. }
  399. //
  400. // clear the perClient value from this variant
  401. //
  402. varElement.Clear ();
  403. varIsTextResource.Clear();
  404. //
  405. // get next client out of the collection
  406. //
  407. hr = pEnumVariant->Next (1, &varElement, &dwElementsLeft);
  408. if (FAILED (hr))
  409. {
  410. SATracePrintf ("CResCtrl::GetLocalUIResources failed on Next:%x",hr);
  411. }
  412. }
  413. return S_OK;
  414. } // end of CResCtrl::GetLocalUIResources
  415. //++--------------------------------------------------------------
  416. //
  417. // Function: InitializeWbemSink
  418. //
  419. // Synopsis: This is the CResCtrl method to initialize the
  420. // component
  421. //
  422. // Arguments: none
  423. //
  424. // Returns: HRESULT - success/failure
  425. //
  426. // History: serdarun Created 01/01/2001
  427. //
  428. //----------------------------------------------------------------
  429. STDMETHODIMP CResCtrl::InitializeWbemSink(void)
  430. {
  431. CComPtr <IWbemLocator> pWbemLocator;
  432. CComBSTR strNetworkRes = CComBSTR(_T("\\\\.\\ROOT\\CIMV2"));
  433. CComBSTR strQueryLang = CComBSTR(_T("WQL"));
  434. CComBSTR strQueryString = CComBSTR(_T("select * from Microsoft_SA_ResourceEvent"));
  435. if ( (strNetworkRes.m_str == NULL) ||
  436. (strQueryLang.m_str == NULL) ||
  437. (strQueryString.m_str == NULL) )
  438. {
  439. SATraceString(" CResCtrl::InitializeWbemSink failed on memory allocation ");
  440. return E_OUTOFMEMORY;
  441. }
  442. //
  443. // create the WBEM locator object
  444. //
  445. HRESULT hr = ::CoCreateInstance (
  446. __uuidof (WbemLocator),
  447. 0, //aggregation pointer
  448. CLSCTX_INPROC_SERVER,
  449. __uuidof (IWbemLocator),
  450. (PVOID*) &pWbemLocator
  451. );
  452. if (SUCCEEDED (hr) && (pWbemLocator.p))
  453. {
  454. //
  455. // connect to WMI
  456. //
  457. hr = pWbemLocator->ConnectServer (
  458. strNetworkRes,
  459. NULL, //user-name
  460. NULL, //password
  461. NULL, //current-locale
  462. 0, //reserved
  463. NULL, //authority
  464. NULL, //context
  465. &m_pWbemServices
  466. );
  467. if (SUCCEEDED (hr))
  468. {
  469. //
  470. // set up the consumer object as the event sync
  471. // for the object type we are interested in
  472. //
  473. hr = m_pWbemServices->ExecNotificationQueryAsync (
  474. strQueryLang,
  475. strQueryString,
  476. 0, //no-status
  477. NULL, //status
  478. (IWbemObjectSink*)(this)
  479. );
  480. if (FAILED (hr))
  481. {
  482. SATracePrintf ("CResCtrl::InitializeWbemSink failed on ExecNotificationQueryAsync:%x",hr);
  483. }
  484. }
  485. else
  486. {
  487. SATracePrintf ("CResCtrl::InitializeWbemSink failed on ConnectServer:%x",hr);
  488. }
  489. }
  490. else
  491. {
  492. SATracePrintf ("CResCtrl::InitializeWbemSink failed on CoCreateInstance:%x",hr);
  493. }
  494. return (hr);
  495. } // end of CResCtrl::InitializeWbemSink method
  496. //++--------------------------------------------------------------
  497. //
  498. // Function: FinalConstruct
  499. //
  500. // Synopsis: This is the CResCtrl method to initialize the
  501. // component
  502. //
  503. // Arguments: none
  504. //
  505. // Returns: HRESULT - success/failure
  506. //
  507. // History: serdarun Created 01/01/2001
  508. //
  509. //----------------------------------------------------------------
  510. STDMETHODIMP CResCtrl::FinalConstruct()
  511. {
  512. HRESULT hr;
  513. //
  514. // initialize the variables
  515. //
  516. m_ResourceMap.clear();
  517. m_MeritMap.clear();
  518. m_lResourceCount = 0;
  519. m_pWbemServices = NULL;
  520. //
  521. // get the local resources
  522. //
  523. hr = GetLocalUIResources();
  524. if (FAILED(hr))
  525. {
  526. SATracePrintf ("CResCtrl::FinalConstruct failed on GetLocalUIResources:%x",hr);
  527. }
  528. //
  529. // register in the wbem sink, if we have any resources
  530. //
  531. if (m_lResourceCount > 0)
  532. {
  533. hr = InitializeWbemSink();
  534. if (FAILED(hr))
  535. {
  536. SATracePrintf ("CResCtrl::FinalConstruct failed on InitializeWbemSink:%x",hr);
  537. //
  538. // returning failure cause component to be destroyed
  539. //
  540. return S_OK;
  541. }
  542. }
  543. return S_OK;
  544. } // end of CResCtrl::FinalConstruct method
  545. //++--------------------------------------------------------------
  546. //
  547. // Function: FinalRelease
  548. //
  549. // Synopsis: This is the CResCtrl method to release the
  550. // resources
  551. //
  552. // Arguments: none
  553. //
  554. // Returns: HRESULT - success/failure
  555. //
  556. // History: serdarun Created 01/01/2001
  557. //
  558. //----------------------------------------------------------------
  559. STDMETHODIMP CResCtrl::FinalRelease()
  560. {
  561. HRESULT hr;
  562. //
  563. // cancel the call for wmi resource events
  564. //
  565. if (m_pWbemServices)
  566. {
  567. hr = m_pWbemServices->CancelAsyncCall ((IWbemObjectSink*)(this));
  568. if (FAILED (hr))
  569. {
  570. SATracePrintf ("CResCtrl::FinalRelease failed on-CancelAsyncCall failed with error:%x:",hr);
  571. }
  572. }
  573. if (m_lResourceCount == 0)
  574. {
  575. return S_OK;
  576. }
  577. //
  578. // release all of the icons if we have any resources
  579. //
  580. ResourceStructPtr ptrResourceStruct = NULL;
  581. ResourceIconMapIterator itrIconMap;
  582. ResourceMapIterator itrResourceMap = m_ResourceMap.begin();
  583. //
  584. // for each resource element
  585. //
  586. while (itrResourceMap != m_ResourceMap.end())
  587. {
  588. ptrResourceStruct = NULL;
  589. //
  590. // get resource information struct
  591. //
  592. ptrResourceStruct = (*itrResourceMap).second;
  593. //
  594. // get the icon map
  595. //
  596. itrIconMap = (ptrResourceStruct->mapResIcon).begin();
  597. while (itrIconMap != (ptrResourceStruct->mapResIcon).end())
  598. {
  599. DestroyIcon((*itrIconMap).second);
  600. itrIconMap++;
  601. }
  602. //
  603. //
  604. //clear the icon map
  605. (ptrResourceStruct->mapResIcon).clear();
  606. itrResourceMap++;
  607. }
  608. m_ResourceMap.clear();
  609. return S_OK;
  610. } // end of CResCtrl::FinalRelease method
  611. //++--------------------------------------------------------------
  612. //
  613. // Function: Indicate
  614. //
  615. // Synopsis: This is the IWbemObjectSink interface method
  616. // through which WBEM calls back to provide the
  617. // event objects
  618. //
  619. // Arguments:
  620. // [in] LONG - number of events
  621. // [in] IWbemClassObject** - array of events
  622. //
  623. // Returns: HRESULT - success/failure
  624. //
  625. // History: serdarun Created 12/10/2000
  626. //
  627. // Called By: WBEM
  628. //
  629. //----------------------------------------------------------------
  630. STDMETHODIMP CResCtrl::Indicate (
  631. /*[in]*/ LONG lObjectCount,
  632. /*[in]*/ IWbemClassObject **ppObjArray
  633. )
  634. {
  635. wstring wsUniqueName = L"";
  636. BOOL bDirty = FALSE;
  637. ResourceMapIterator itrResourceMap = NULL;
  638. CComBSTR bstrUniqueName = CComBSTR(UNIQUE_NAME);
  639. CComBSTR bstrDisplayInfo = CComBSTR(DISPLAY_INFORMATION);
  640. if ( (bstrUniqueName.m_str == NULL) ||
  641. (bstrDisplayInfo.m_str == NULL) )
  642. {
  643. SATraceString(" CResCtrl::Indicate failed on memory allocation ");
  644. return WBEM_NO_ERROR;
  645. }
  646. // Get the info from the object.
  647. // =============================
  648. try
  649. {
  650. for (long i = 0; i < lObjectCount; i++)
  651. {
  652. itrResourceMap = NULL;
  653. IWbemClassObject *pObj = ppObjArray[i];
  654. //
  655. // get the unique name
  656. //
  657. CComVariant vUniqueName;
  658. pObj->Get(bstrUniqueName, 0, &vUniqueName, 0, 0);
  659. wsUniqueName = V_BSTR(&vUniqueName);
  660. //
  661. // If here, we know the object is one of the kind we asked for.
  662. //
  663. itrResourceMap = m_ResourceMap.find(wsUniqueName);
  664. ResourceStructPtr ptrResourceStruct = NULL;
  665. if ( (itrResourceMap != NULL) )
  666. {
  667. ptrResourceStruct = (*itrResourceMap).second;
  668. //
  669. // get the new display state
  670. //
  671. CComVariant vDisplayInformationID;
  672. pObj->Get(bstrDisplayInfo, 0, &vDisplayInformationID, 0, 0);
  673. if (ptrResourceStruct)
  674. {
  675. //
  676. // if new state is different set dirty flag
  677. //
  678. if (ptrResourceStruct->lState != vDisplayInformationID.lVal)
  679. {
  680. ptrResourceStruct->lState = vDisplayInformationID.lVal;
  681. bDirty = TRUE;
  682. }
  683. }
  684. }
  685. }
  686. //
  687. // force a repaint
  688. //
  689. if (bDirty)
  690. {
  691. FireViewChange();
  692. }
  693. }
  694. catch(...)
  695. {
  696. SATraceString("CResCtrl::Indicate, unknown exception occured");
  697. }
  698. return WBEM_NO_ERROR;
  699. } // end of CResCtrl::Indicate method
  700. //++--------------------------------------------------------------
  701. //
  702. // Function: SetStatus
  703. //
  704. // Synopsis: This is the IWbemObjectSink interface method
  705. // through which WBEM calls in to indicate end of
  706. // event sequence or provide other error codes
  707. //
  708. // Arguments:
  709. // [in] LONG - progress
  710. // [in] HRESULT - status information
  711. // [in] BSTR - string info
  712. // [in] IWbemClassObject* - status object
  713. //
  714. // Returns: HRESULT - success/failure
  715. //
  716. // History: serdarun Created 12/10/2000
  717. //
  718. // Called By: WBEM
  719. //
  720. //----------------------------------------------------------------
  721. STDMETHODIMP CResCtrl::SetStatus (
  722. /*[in]*/ LONG lFlags,
  723. /*[in]*/ HRESULT hResult,
  724. /*[in]*/ BSTR strParam,
  725. /*[in]*/ IWbemClassObject *pObjParam
  726. )
  727. {
  728. SATracePrintf ("SAConsumer-IWbemObjectSink::SetStatus called:%x",hResult);
  729. return (WBEM_S_NO_ERROR);
  730. } // end of CResCtrl::SetStatus method
  731. //++--------------------------------------------------------------
  732. //
  733. // Function: OnDraw
  734. //
  735. // Synopsis: Method used to draw the icons
  736. //
  737. //
  738. // Returns: HRESULT - success/failure
  739. //
  740. // History: serdarun Created 12/10/2000
  741. //
  742. //----------------------------------------------------------------
  743. HRESULT CResCtrl::OnDraw(ATL_DRAWINFO& di)
  744. {
  745. //
  746. // get the drawing rectangle
  747. //
  748. RECT& rc = *(RECT*)di.prcBounds;
  749. //
  750. // position of the icon from left
  751. //
  752. int iLeft = 0;
  753. ResourceStructPtr ptrResourceStruct = NULL;
  754. ResourceIconMapIterator itrIconMap = NULL;
  755. //
  756. // iterator for resource
  757. //
  758. ResourceMapIterator itrResourceMap = m_ResourceMap.end();
  759. MeritMapIterator itrMeritMap = m_MeritMap.begin();
  760. //
  761. // for each resource in merit map
  762. //
  763. while (itrMeritMap != m_MeritMap.end())
  764. {
  765. //
  766. // find the resource in resource map
  767. //
  768. itrResourceMap = m_ResourceMap.find((*itrMeritMap).second);
  769. //
  770. // if it is not in the map, continue with the next item
  771. //
  772. if (itrResourceMap == m_ResourceMap.end())
  773. {
  774. itrMeritMap++;
  775. continue;
  776. }
  777. ptrResourceStruct = NULL;
  778. //
  779. // get resource information struct
  780. //
  781. ptrResourceStruct = (*itrResourceMap).second;
  782. if (NULL != ptrResourceStruct)
  783. {
  784. //
  785. // find the icon corresponding to the state
  786. //
  787. itrIconMap = (ptrResourceStruct->mapResIcon).find(ptrResourceStruct->lState);
  788. if (itrIconMap != (ptrResourceStruct->mapResIcon).end())
  789. {
  790. //
  791. // calculate the position and draw
  792. //
  793. DrawIconEx(
  794. di.hdcDraw, // handle to device context
  795. rc.left+iLeft, // x-coord of upper left corner
  796. rc.top, // y-coord of upper left corner
  797. (*itrIconMap).second, // handle to icon
  798. 0, // icon width
  799. 0, // icon height
  800. 0, // frame index, animated cursor
  801. NULL, // handle to background brush
  802. DI_NORMAL // icon-drawing flags
  803. );
  804. }
  805. }
  806. itrMeritMap++;
  807. iLeft = iLeft + 16;
  808. }
  809. return S_OK;
  810. } // end of CResCtrl::OnDraw method
  811. //++--------------------------------------------------------------
  812. //
  813. // Function: ExpandSz
  814. //
  815. // Synopsis: This is the CResCtrl class object method
  816. // used to get the directory where the resource dlls are
  817. // present
  818. //
  819. // Arguments: [out] wstring& - directory path
  820. //
  821. // Returns: HRESULT - success/failure
  822. //
  823. // History: serdarun Created 01/16/2001
  824. //
  825. //----------------------------------------------------------------
  826. HRESULT CResCtrl::ExpandSz(IN const TCHAR *lpszStr, OUT LPTSTR *ppszStr)
  827. {
  828. DWORD dwBufSize = 0;
  829. _ASSERT(lpszStr);
  830. _ASSERT(ppszStr);
  831. _ASSERT(NULL==(*ppszStr));
  832. if ((NULL==lpszStr) || (NULL==ppszStr) ||
  833. (NULL != (*ppszStr)))
  834. {
  835. return E_INVALIDARG;
  836. }
  837. dwBufSize = ExpandEnvironmentStrings(lpszStr,
  838. (*ppszStr),
  839. dwBufSize);
  840. _ASSERT(0 != dwBufSize);
  841. (*ppszStr) = (LPTSTR)SaAlloc(dwBufSize * sizeof(TCHAR) );
  842. if (NULL == (*ppszStr))
  843. {
  844. SATraceString("MemAlloc failed in ExpandSz");
  845. return E_OUTOFMEMORY;
  846. }
  847. ExpandEnvironmentStrings(lpszStr,
  848. (*ppszStr),
  849. dwBufSize);
  850. SATracePrintf("Expanded string is \'%ws\'", (*ppszStr));
  851. return S_OK;
  852. } // end of CResCtrl::ExpandSz method
  853. //++--------------------------------------------------------------
  854. //
  855. // Function: SetLangID
  856. //
  857. // Synopsis: This is the CResCtrl class object method
  858. // used to set the language id
  859. //
  860. // Arguments: [out] DOWRD* - language id
  861. //
  862. // Returns: HRESULT - success/failure
  863. //
  864. // History: serdarun Created 01/16/2001
  865. //
  866. //----------------------------------------------------------------
  867. void CResCtrl::SetLangID(DWORD * dwLangID)
  868. {
  869. DWORD dwErr, dwNewLangID, dwCurLangID;
  870. CRegKey crKey;
  871. int iConversion;
  872. iConversion = swscanf(DEFAULT_LANGID, TEXT("%X"), dwLangID);
  873. if (iConversion != 1)
  874. {
  875. *dwLangID = 0;
  876. return;
  877. }
  878. dwErr = crKey.Open(HKEY_LOCAL_MACHINE, RESOURCE_REGISTRY_PATH);
  879. if (dwErr != ERROR_SUCCESS)
  880. {
  881. SATracePrintf("RegOpen(2) failed %ld in SetLangID", dwErr);
  882. return;
  883. }
  884. dwCurLangID = 0;
  885. dwErr = crKey.QueryValue(dwCurLangID, LANGID_VALUE);
  886. if (ERROR_SUCCESS != dwErr)
  887. {
  888. SATracePrintf("QueryValue(CUR_LANGID_VALUE) failed %ld in SetLangID", dwErr);
  889. return;
  890. }
  891. else
  892. {
  893. *dwLangID = dwCurLangID;
  894. }
  895. } // end of CResCtrl::SetLangID method
  896. //++--------------------------------------------------------------
  897. //
  898. // Function: GetResourceDirectory
  899. //
  900. // Synopsis: This is the CResCtrl class object method
  901. // used to get the directory where the resource dlls are
  902. // present
  903. //
  904. // Arguments: [out] wstring& - directory path
  905. //
  906. // Returns: HRESULT - success/failure
  907. //
  908. // History: serdarun Created 01/16/2001
  909. //
  910. //----------------------------------------------------------------
  911. HRESULT CResCtrl::GetResourceDirectory (
  912. /*[out]*/ wstring& wstrResourceDir
  913. )
  914. {
  915. TCHAR szLangId[10];
  916. LPTSTR lpszExStr=NULL;
  917. DWORD dwErr;
  918. DWORD dwRead = 0;
  919. HRESULT hr = S_OK;
  920. TCHAR szResourceDirectory[MAX_PATH];
  921. BOOL bRetVal = FALSE;
  922. DWORD dwLangID;
  923. do
  924. {
  925. //
  926. // get the language id from registry
  927. //
  928. SetLangID(&dwLangID);
  929. CComVariant vtPath;
  930. //
  931. // get the resource path from the registry
  932. //
  933. CRegKey crKey;
  934. dwErr = crKey.Open(HKEY_LOCAL_MACHINE, RESOURCE_REGISTRY_PATH);
  935. if (dwErr != ERROR_SUCCESS)
  936. {
  937. SATracePrintf("RegOpen failed %ld in GetResourceDirectory", dwErr);
  938. }
  939. else
  940. {
  941. dwErr = crKey.QueryValue(szResourceDirectory, RESOURCE_DIRECTORY,&dwRead);
  942. if ( (ERROR_SUCCESS != dwErr) || (dwRead == 0) )
  943. {
  944. SATracePrintf("QueryValue(RESOURCE_DIRECTORY) failed %ld in GetResourceDirectory", dwErr);
  945. }
  946. else
  947. {
  948. bRetVal = TRUE;
  949. }
  950. }
  951. if (!bRetVal)
  952. {
  953. SATraceString ("CResCtrl::GetResourceDirectory unable to obtain resource dir path");
  954. wstrResourceDir.assign (DEFAULT_DIRECTORY);
  955. }
  956. else
  957. {
  958. wstrResourceDir.assign (szResourceDirectory);
  959. }
  960. hr = ExpandSz(wstrResourceDir.data(), &lpszExStr);
  961. if (FAILED(hr))
  962. {
  963. wstrResourceDir.assign (DEFAULT_EXPANDED_DIRECTORY);
  964. }
  965. else
  966. {
  967. wstrResourceDir.assign(lpszExStr);
  968. SaFree(lpszExStr);
  969. lpszExStr = NULL;
  970. }
  971. wstrResourceDir.append (DELIMITER);
  972. wsprintf(szLangId, TEXT("%04X"), dwLangID);
  973. wstrResourceDir.append (szLangId);
  974. wstrResourceDir.append (DELIMITER);
  975. SATracePrintf ("CResCtrl::GetResourceDirectory has set LANGID to:%d", dwLangID);
  976. //
  977. // success
  978. //
  979. SATracePrintf ("CResCtrl::GetResourceDirectory determined resource directory:'%ws'",wstrResourceDir.data ());
  980. }
  981. while (false);
  982. return (hr);
  983. } // end of CResCtrl::GetResourceDirectory method
  984. //++--------------------------------------------------------------
  985. //
  986. // Function: HexCharToULong
  987. //
  988. // Synopsis: converts a hex digit to base 10 number
  989. //
  990. //
  991. // Returns: ULONG
  992. //
  993. // History: serdarun Created 12/10/2000
  994. //
  995. //----------------------------------------------------------------
  996. ULONG CResCtrl::HexCharToULong(WCHAR wch)
  997. {
  998. if ((wch >= '0') && (wch <= '9') )
  999. {
  1000. return ULONG(wch - '0');
  1001. }
  1002. if ((wch >= 'A') && (wch <= 'F') )
  1003. {
  1004. return ULONG(wch - 'A' + 10);
  1005. }
  1006. if ((wch >= 'a') && (wch <= 'f') )
  1007. {
  1008. return ULONG(wch - 'a' + 10);
  1009. }
  1010. return 0;
  1011. }
  1012. //++--------------------------------------------------------------
  1013. //
  1014. // Function: HexStringToULong
  1015. //
  1016. // Synopsis: converts a hex string to unsigned long
  1017. //
  1018. //
  1019. // Returns: ULONG
  1020. //
  1021. // History: serdarun Created 12/10/2000
  1022. //
  1023. //----------------------------------------------------------------
  1024. ULONG CResCtrl::HexStringToULong(wstring wsHexString)
  1025. {
  1026. int iLength;
  1027. int iIndex = 0;
  1028. ULONG ulResult = 0;
  1029. iLength = wsHexString.size();
  1030. while (iIndex < iLength)
  1031. {
  1032. ulResult *= 16;
  1033. ulResult += HexCharToULong(wsHexString[iIndex]);
  1034. iIndex++;
  1035. }
  1036. return ulResult;
  1037. }