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.

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