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.

157 lines
4.0 KiB

  1. //TitleDB.cpp
  2. #include "TitleDB.h"
  3. #define PERFLIB TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\PerfLib")
  4. ///////////////////////////////////////////////////////////////////////////////
  5. //
  6. // CTitleLibrary
  7. //
  8. // The CCtrLibrary object is an object that evaluates object and counter
  9. // titles based on their IDs. The object uses a lookup table, where the
  10. // elements reference strings in a block of ID / title pairs and the index
  11. // is the ID values of the object and counters.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. CTitleLibrary::CTitleLibrary()
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Constructor
  18. //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. {
  21. Initialize();
  22. }
  23. CTitleLibrary::~CTitleLibrary()
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Destructor
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. {
  30. if (m_tcsDataBlock)
  31. delete []m_tcsDataBlock;
  32. if (m_atcsNames)
  33. delete []m_atcsNames;
  34. }
  35. HRESULT CTitleLibrary::Initialize()
  36. ///////////////////////////////////////////////////////////////////////////////
  37. //
  38. // Initialize
  39. //
  40. // Sets up the lookup table for the library. The titles are indexed by their
  41. // key ID values.
  42. //
  43. // Determine the maximum index value of the titles. Attempt to query the
  44. // title/index pairs to determine how large of a block must be allocated to
  45. // accept the structure. Create the block, and then query the title\index
  46. // pairs. Create a lookup table the size of the maximum index, and populate
  47. // it with the retrieved title data.
  48. //
  49. ///////////////////////////////////////////////////////////////////////////////
  50. {
  51. HKEY hKey = 0;
  52. // Get the upper index limit
  53. // =========================
  54. DWORD dwSize;
  55. if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, PERFLIB,
  56. 0, KEY_READ, &hKey))
  57. {
  58. return E_FAIL;
  59. }
  60. dwSize = 4;
  61. if (ERROR_SUCCESS != RegQueryValueEx(hKey, "Last Counter",
  62. 0, 0, (PBYTE)&m_lMaxIndex, &dwSize))
  63. {
  64. RegCloseKey(hKey);
  65. return E_FAIL;
  66. }
  67. RegCloseKey(hKey);
  68. // Get the size of block required to retrieve the title / index pairs
  69. // ==================================================================
  70. if (ERROR_SUCCESS != RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT("Counter009"),
  71. 0, 0, 0, &dwSize))
  72. return E_FAIL;
  73. // Allocate the block, and retrieve the title / index pairs
  74. // ========================================================
  75. m_tcsDataBlock = new TCHAR[dwSize];
  76. if (ERROR_SUCCESS != RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT("Counter009"),
  77. 0, 0, (PBYTE)m_tcsDataBlock, &dwSize))
  78. {
  79. delete []m_tcsDataBlock;
  80. return E_FAIL;
  81. }
  82. // Allocate and clear the memory for the lookup table
  83. // ==================================================
  84. m_atcsNames = new TCHAR*[m_lMaxIndex + 1];
  85. memset(m_atcsNames, 0, (sizeof(TCHAR*) * (m_lMaxIndex + 1)));
  86. // Populate the lookup table
  87. // =========================
  88. TCHAR* tcsTemp = m_tcsDataBlock;
  89. int nLen, nIndex;
  90. while ( 0 != (nLen = lstrlen(tcsTemp)))
  91. {
  92. // Get the index
  93. // =============
  94. nIndex = _ttoi(tcsTemp);
  95. tcsTemp += nLen + 1;
  96. // Set the table element at the index value to the string pointer
  97. // ==============================================================
  98. m_atcsNames[nIndex] = tcsTemp;
  99. tcsTemp += lstrlen(tcsTemp) + 1;
  100. }
  101. return S_OK;
  102. }
  103. HRESULT CTitleLibrary::GetName(long lID, TCHAR** ptcsName)
  104. ///////////////////////////////////////////////////////////////////////////////
  105. //
  106. // GetName
  107. //
  108. // Evaluates the title given an object's or counter's ID.
  109. //
  110. // Parameters:
  111. // lID - the Index into the library
  112. // pstrName - the Name
  113. //
  114. ///////////////////////////////////////////////////////////////////////////////
  115. {
  116. // Is it a valid index?
  117. // ====================
  118. if (lID > m_lMaxIndex)
  119. return E_INVALIDARG;
  120. // Assign the pointer
  121. // ==================
  122. *ptcsName = m_atcsNames[lID];
  123. return S_OK;
  124. }