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.

383 lines
7.7 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. PERFNDB.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <stdio.h>
  10. #include <wbemcli.h>
  11. #include <cominit.h>
  12. #include <winmgmtr.h>
  13. #include <tchar.h>
  14. #include "perfndb.h"
  15. #include "adaputil.h"
  16. #include "adapreg.h"
  17. CPerfNameDb::CPerfNameDb(HKEY hKey):
  18. m_pMultiCounter(NULL),
  19. m_pMultiHelp(NULL),
  20. m_pCounter(NULL),
  21. m_pHelp(NULL),
  22. m_Size(0),
  23. m_fOk(FALSE)
  24. {
  25. try {
  26. m_fOk = SUCCEEDED(Init(hKey));
  27. }
  28. catch (...)
  29. {
  30. CAdapUtility::NTLogEvent( EVENTLOG_WARNING_TYPE,
  31. WBEM_MC_ADAP_PERFLIB_SUBKEY_FAILURE,
  32. L"HKEY_PERFORMANCE_xxxTEXT",
  33. CHex( ::GetLastError() ));
  34. }
  35. //Dump();
  36. }
  37. HRESULT
  38. CPerfNameDb::Init(HKEY hKey)
  39. {
  40. LONG lRet;
  41. DWORD dwType;
  42. DWORD dwInc = 0x10000;
  43. DWORD dwSize = dwInc;
  44. BYTE * pData;
  45. DWORD SizeCounter;
  46. DWORD SizeHelp;
  47. WCHAR * pEnd;
  48. DWORD Index;
  49. HRESULT hr = S_FALSE;
  50. pData = new BYTE[dwSize];
  51. if (!pData)
  52. return WBEM_E_OUT_OF_MEMORY;
  53. retry1:
  54. {
  55. lRet = RegQueryValueEx(hKey,
  56. _T("Counter"),
  57. NULL,
  58. &dwType,
  59. pData,
  60. &dwSize);
  61. if (ERROR_SUCCESS == lRet)
  62. {
  63. m_pMultiCounter = (WCHAR *)pData;
  64. pData = NULL;
  65. hr = S_OK;
  66. SizeCounter = dwSize;
  67. }
  68. else if (ERROR_MORE_DATA == lRet)
  69. {
  70. delete [] pData;
  71. pData = new BYTE[dwSize];
  72. if (!pData)
  73. {
  74. hr = WBEM_E_OUT_OF_MEMORY;
  75. goto cleanup;
  76. }
  77. else
  78. {
  79. goto retry1;
  80. }
  81. }
  82. else
  83. {
  84. hr = WBEM_E_FAILED;
  85. goto cleanup;
  86. }
  87. };
  88. if (S_OK != hr) // we exited the loop without openeing the DB
  89. goto cleanup;
  90. hr = S_FALSE;
  91. dwSize = dwInc;
  92. //
  93. // start a new loop for the help texts
  94. //
  95. pData = new BYTE[dwSize];
  96. if (!pData)
  97. return WBEM_E_OUT_OF_MEMORY;
  98. retry2:
  99. {
  100. lRet = RegQueryValueEx(hKey,
  101. _T("Help"),
  102. NULL,
  103. &dwType,
  104. pData,
  105. &dwSize);
  106. if (ERROR_SUCCESS == lRet)
  107. {
  108. m_pMultiHelp = (WCHAR *)pData;
  109. pData = NULL;
  110. hr = S_OK;
  111. SizeHelp = dwSize;
  112. }
  113. else if (ERROR_MORE_DATA == lRet)
  114. {
  115. delete [] pData;
  116. pData = new BYTE[dwSize];
  117. if (!pData)
  118. {
  119. hr = WBEM_E_OUT_OF_MEMORY;
  120. goto cleanup;
  121. }
  122. else
  123. {
  124. goto retry2;
  125. }
  126. }
  127. else
  128. {
  129. hr = WBEM_E_FAILED;
  130. goto cleanup;
  131. }
  132. };
  133. if (S_OK != hr) // we exited the loop without openeing the DB
  134. goto cleanup;
  135. //
  136. // now parse the string, and set-up the arrays
  137. //
  138. pEnd = (WCHAR *)(((ULONG_PTR)m_pMultiCounter)+SizeCounter);
  139. // points to the last char
  140. pEnd--;
  141. while (*pEnd == L'\0')
  142. pEnd--;
  143. while (*pEnd)
  144. pEnd--;
  145. // past the zero after the last index
  146. pEnd--;
  147. while (*pEnd)
  148. pEnd--;
  149. // this should point to the last index as a string
  150. pEnd++;
  151. Index = _wtoi(pEnd);
  152. if (Index)
  153. {
  154. Index+=2; // just to be safe
  155. m_Size = Index;
  156. m_pCounter = new WCHAR*[Index];
  157. if (!m_pCounter){
  158. hr = WBEM_E_OUT_OF_MEMORY;
  159. goto cleanup;
  160. }
  161. memset(m_pCounter,0,Index*sizeof(WCHAR *));
  162. m_pHelp = new WCHAR*[Index];
  163. if (!m_pHelp){
  164. hr = WBEM_E_OUT_OF_MEMORY;
  165. goto cleanup;
  166. }
  167. memset(m_pHelp,0,Index*sizeof(WCHAR *));
  168. DWORD IndexCounter;
  169. DWORD IndexHelp;
  170. WCHAR * pStartCounter = m_pMultiCounter;
  171. WCHAR * pStartHelp = m_pMultiHelp;
  172. ULONG_PTR LimitMultiCounter = (ULONG_PTR)m_pMultiCounter + SizeCounter;
  173. ULONG_PTR LimitMultiHelp = (ULONG_PTR)m_pMultiHelp + SizeHelp;
  174. while ((*pStartCounter) && ((ULONG_PTR)pStartCounter < LimitMultiCounter))
  175. {
  176. IndexCounter = _wtoi(pStartCounter);
  177. while(*pStartCounter)
  178. pStartCounter++;
  179. pStartCounter++; // points to the string
  180. if (IndexCounter && (IndexCounter < Index))
  181. m_pCounter[IndexCounter] = pStartCounter;
  182. // skip the string
  183. while(*pStartCounter)
  184. pStartCounter++;
  185. pStartCounter++; // points to the next number
  186. }
  187. while((*pStartHelp) && ((ULONG_PTR)pStartHelp < LimitMultiHelp))
  188. {
  189. IndexHelp = _wtoi(pStartHelp);
  190. while(*pStartHelp)
  191. pStartHelp++;
  192. pStartHelp++; // points to the string
  193. if (IndexHelp && (IndexHelp < Index))
  194. m_pHelp[IndexHelp] = pStartHelp;
  195. // skip the string
  196. while(*pStartHelp)
  197. pStartHelp++;
  198. pStartHelp++; // points to the next number
  199. }
  200. hr = S_OK;
  201. }
  202. else
  203. {
  204. hr = WBEM_E_FAILED;
  205. }
  206. cleanup:
  207. if (pData)
  208. delete [] pData;
  209. return hr;
  210. }
  211. HRESULT
  212. CPerfNameDb::GetDisplayName(DWORD dwIndex,
  213. WString& wstrDisplayName )
  214. {
  215. HRESULT hr;
  216. if (dwIndex < m_Size)
  217. {
  218. try {
  219. // Check for a vaild display name
  220. if (m_pCounter[dwIndex]) {
  221. wstrDisplayName = m_pCounter[dwIndex];
  222. hr = WBEM_S_NO_ERROR;
  223. } else {
  224. hr = WBEM_E_FAILED;
  225. }
  226. } catch (...) {
  227. hr = WBEM_E_OUT_OF_MEMORY;
  228. }
  229. } else {
  230. hr = WBEM_E_INVALID_PARAMETER;
  231. }
  232. return hr;
  233. }
  234. HRESULT
  235. CPerfNameDb::GetDisplayName(DWORD dwIndex,
  236. LPCWSTR* ppwcsDisplayName )
  237. {
  238. HRESULT hr;
  239. if (dwIndex < m_Size && ppwcsDisplayName)
  240. {
  241. // Check for a vaild display name
  242. if (m_pCounter[dwIndex])
  243. {
  244. *ppwcsDisplayName = m_pCounter[dwIndex];
  245. hr = WBEM_S_NO_ERROR;
  246. }
  247. else
  248. {
  249. hr = WBEM_E_FAILED;
  250. }
  251. } else {
  252. hr = WBEM_E_INVALID_PARAMETER;
  253. }
  254. return hr;
  255. }
  256. HRESULT
  257. CPerfNameDb::GetHelpName( DWORD dwIndex, WString& wstrHelpName )
  258. {
  259. HRESULT hr;
  260. if (dwIndex < m_Size)
  261. {
  262. try {
  263. // Check for a vaild display name
  264. if (m_pHelp[dwIndex]) {
  265. wstrHelpName = m_pHelp[dwIndex];
  266. hr = WBEM_S_NO_ERROR;
  267. } else {
  268. hr = WBEM_E_FAILED;
  269. }
  270. } catch (...) {
  271. hr = WBEM_E_OUT_OF_MEMORY;
  272. }
  273. } else {
  274. hr = WBEM_E_INVALID_PARAMETER;
  275. }
  276. return hr;
  277. };
  278. HRESULT
  279. CPerfNameDb::GetHelpName( DWORD dwIndex, LPCWSTR* ppwcsHelpName )
  280. {
  281. HRESULT hr;
  282. if (dwIndex < m_Size && ppwcsHelpName)
  283. {
  284. // Check for a vaild display name
  285. if (m_pHelp[dwIndex])
  286. {
  287. *ppwcsHelpName = m_pHelp[dwIndex];
  288. hr = WBEM_S_NO_ERROR;
  289. }
  290. else
  291. {
  292. hr = WBEM_E_FAILED;
  293. }
  294. } else {
  295. hr = WBEM_E_INVALID_PARAMETER;
  296. }
  297. return hr;
  298. };
  299. #ifdef _DUMP_DATABASE_NAME_
  300. VOID
  301. CPerfNameDb::Dump()
  302. {
  303. if (m_pHelp && m_pCounter)
  304. {
  305. WCHAR pBuff[2048];
  306. DWORD i;
  307. for (i=0;i<m_Size;i++)
  308. {
  309. if (m_pCounter[i])
  310. {
  311. wsprintfW(pBuff,L"%d %s\n",i,m_pCounter[i]);
  312. OutputDebugStringW(pBuff);
  313. }
  314. }
  315. for (i=0;i<m_Size;i++)
  316. {
  317. if (m_pHelp[i])
  318. {
  319. if (lstrlenW(m_pHelp[i]) > 100)
  320. {
  321. m_pHelp[i][100]=0;
  322. }
  323. wsprintfW(pBuff,L"%d %s\n",i,m_pHelp[i]);
  324. OutputDebugStringW(pBuff);
  325. }
  326. }
  327. }
  328. };
  329. #endif
  330. CPerfNameDb::~CPerfNameDb()
  331. {
  332. if (m_pMultiCounter)
  333. delete [] m_pMultiCounter;
  334. if (m_pMultiHelp)
  335. delete [] m_pMultiHelp;
  336. if (m_pCounter)
  337. delete [] m_pCounter;
  338. if (m_pHelp)
  339. delete [] m_pHelp;
  340. }