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.

120 lines
2.8 KiB

  1. //=============================================================================
  2. // The CResourceMap class is useful for several data categories. It contains
  3. // a map of the Win32_PnPAllocatedResource class.
  4. //=============================================================================
  5. #include "stdafx.h"
  6. #include "resourcemap.h"
  7. CResourceMap::CResourceMap() : m_dwInitTime(0), m_hr(S_OK) { }
  8. CResourceMap::~CResourceMap()
  9. {
  10. Empty();
  11. }
  12. void CResourceMap::Empty()
  13. {
  14. CString strKey;
  15. CStringList * plistStrings;
  16. for (POSITION pos = m_map.GetStartPosition(); pos != NULL;)
  17. {
  18. m_map.GetNextAssoc(pos, strKey, (CObject*&) plistStrings);
  19. if (plistStrings)
  20. delete plistStrings;
  21. }
  22. m_map.RemoveAll();
  23. }
  24. HRESULT CResourceMap::Initialize(CWMIHelper * pWMIHelper)
  25. {
  26. if (m_dwInitTime)
  27. return m_hr;
  28. ASSERT(pWMIHelper);
  29. Empty();
  30. CString aAssociationClasses[] =
  31. {
  32. _T("Win32_PnPAllocatedResource"),
  33. _T("Win32_CIMLogicalDeviceCIMDataFile"),
  34. _T("")
  35. };
  36. for (int j = 0; !aAssociationClasses[j].IsEmpty(); j++)
  37. {
  38. // Enumerate the class, inserting each Antecedent, Dependent pair into the map.
  39. CString strAntecedent, strDependent, strQuery;
  40. CStringList * pstringlist;
  41. CWMIObjectCollection * pCollection = NULL;
  42. HRESULT hr = pWMIHelper->Enumerate(aAssociationClasses[j], &pCollection);
  43. if (SUCCEEDED(hr))
  44. {
  45. CWMIObject * pObject = NULL;
  46. while (S_OK == pCollection->GetNext(&pObject))
  47. {
  48. strAntecedent.Empty();
  49. strDependent.Empty();
  50. if (SUCCEEDED(pObject->GetValueString(_T("Antecedent"), &strAntecedent)) &&
  51. SUCCEEDED(pObject->GetValueString(_T("Dependent"), &strDependent)))
  52. {
  53. // Strip off the machine and namespace (too many ways for these to be formatted).
  54. int i = strAntecedent.Find(_T(":"));
  55. if (i != -1)
  56. strAntecedent = strAntecedent.Right(strAntecedent.GetLength() - i - 1);
  57. i = strDependent.Find(_T(":"));
  58. if (i != -1)
  59. strDependent = strDependent.Right(strDependent.GetLength() - i - 1);
  60. pstringlist = Lookup(strAntecedent);
  61. if (!pstringlist)
  62. {
  63. pstringlist = new CStringList;
  64. if (pstringlist)
  65. m_map.SetAt(strAntecedent, (CObject *) pstringlist);
  66. }
  67. if (pstringlist)
  68. pstringlist->AddTail(strDependent);
  69. pstringlist = Lookup(strDependent);
  70. if (!pstringlist)
  71. {
  72. pstringlist = new CStringList;
  73. if (pstringlist)
  74. m_map.SetAt(strDependent, (CObject *) pstringlist);
  75. }
  76. if (pstringlist)
  77. pstringlist->AddTail(strAntecedent);
  78. }
  79. }
  80. delete pObject;
  81. delete pCollection;
  82. }
  83. else
  84. {
  85. m_hr = hr;
  86. break;
  87. }
  88. }
  89. m_dwInitTime = ::GetTickCount();
  90. return m_hr;
  91. }
  92. CStringList * CResourceMap::Lookup(const CString & strKey)
  93. {
  94. CStringList * plistStrings;
  95. if (m_map.Lookup(strKey, (CObject*&) plistStrings))
  96. return plistStrings;
  97. else
  98. return NULL;
  99. }