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.

811 lines
32 KiB

  1. /*
  2. WbemUtil.C
  3. functions and definitions used to access WBEM perf
  4. data
  5. */
  6. #include <windows.h>
  7. #include <rpc.h>
  8. #include <ntprfctr.h>
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include "wbemutil.h"
  12. const PERFTYPE_LOOKUP PerfTypeTable[] =
  13. {
  14. {PERF_100NSEC_MULTI_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  15. {PERF_100NSEC_MULTI_TIMER_INV, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  16. {PERF_100NSEC_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  17. {PERF_100NSEC_TIMER_INV, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  18. {PERF_AVERAGE_BASE, (LPWSTR)L"uint32" ,(LPWSTR)L""},
  19. {PERF_AVERAGE_BULK, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  20. {PERF_AVERAGE_TIMER, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  21. {PERF_COUNTER_BULK_COUNT, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  22. {PERF_COUNTER_COUNTER, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  23. {PERF_COUNTER_DELTA, (LPWSTR)L"uint32" ,(LPWSTR)L"uint32"},
  24. {PERF_COUNTER_HISTOGRAM_TYPE, (LPWSTR)L"" ,(LPWSTR)L""},
  25. {PERF_COUNTER_LARGE_DELTA, (LPWSTR)L"uint64" ,(LPWSTR)L"uint64"},
  26. {PERF_COUNTER_LARGE_QUEUELEN_TYPE, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  27. {PERF_COUNTER_LARGE_RAWCOUNT, (LPWSTR)L"uint64" ,(LPWSTR)L"uint64"},
  28. {PERF_COUNTER_LARGE_RAWCOUNT_HEX, (LPWSTR)L"uint64",(LPWSTR)L"uint64"},
  29. {PERF_COUNTER_MULTI_BASE, (LPWSTR)L"uint64" ,(LPWSTR)L""},
  30. {PERF_COUNTER_MULTI_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  31. {PERF_COUNTER_MULTI_TIMER_INV, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  32. {PERF_COUNTER_NODATA, (LPWSTR)L"" ,(LPWSTR)L""},
  33. {PERF_COUNTER_QUEUELEN_TYPE, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  34. {PERF_COUNTER_RAWCOUNT, (LPWSTR)L"uint32" ,(LPWSTR)L"uint32"},
  35. {PERF_COUNTER_RAWCOUNT_HEX, (LPWSTR)L"uint32" ,(LPWSTR)L"uint32"},
  36. {PERF_COUNTER_TEXT, (LPWSTR)L"string" ,(LPWSTR)L"string"},
  37. {PERF_COUNTER_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  38. {PERF_COUNTER_TIMER_INV, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  39. {PERF_ELAPSED_TIME, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  40. {PERF_RAW_BASE, (LPWSTR)L"uint32" ,(LPWSTR)L""},
  41. {PERF_RAW_FRACTION, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  42. {PERF_SAMPLE_FRACTION, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  43. {PERF_SAMPLE_BASE, (LPWSTR)L"uint32" ,(LPWSTR)L""},
  44. {PERF_SAMPLE_COUNTER, (LPWSTR)L"uint32" ,(LPWSTR)L"real64"},
  45. {PERF_COUNTER_100NS_QUEUELEN_TYPE, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  46. {PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  47. {PERF_OBJ_TIME_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  48. {PERF_LARGE_RAW_BASE, (LPWSTR)L"uint64" ,(LPWSTR)L""},
  49. {PERF_LARGE_RAW_FRACTION, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  50. {PERF_PRECISION_SYSTEM_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  51. {PERF_PRECISION_100NS_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"},
  52. {PERF_PRECISION_OBJECT_TIMER, (LPWSTR)L"uint64" ,(LPWSTR)L"real64"}
  53. };
  54. const PPERFTYPE_LOOKUP PerfTypes = (const PPERFTYPE_LOOKUP)&PerfTypeTable[0];
  55. const DWORD dwNumPerfTypes = (sizeof(PerfTypeTable) / sizeof(PerfTypeTable[0]));
  56. const PERFOBJECT_LOOKUP PerfObjectGuidTable[] =
  57. {
  58. {SYSTEM_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F5C-9E4D-11d1-BB3C-00A0C913CAD4}"},
  59. {PROCESSOR_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F5D-9E4D-11d1-BB3C-00A0C913CAD4}"},
  60. {MEMORY_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F5E-9E4D-11d1-BB3C-00A0C913CAD4}"},
  61. {CACHE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F5F-9E4D-11d1-BB3C-00A0C913CAD4}"},
  62. {PHYSICAL_DISK_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F60-9E4D-11d1-BB3C-00A0C913CAD4}"},
  63. {LOGICAL_DISK_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F61-9E4D-11d1-BB3C-00A0C913CAD4}"},
  64. {PROCESS_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F62-9E4D-11d1-BB3C-00A0C913CAD4}"},
  65. {THREAD_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F63-9E4D-11d1-BB3C-00A0C913CAD4}"},
  66. {OBJECT_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F64-9E4D-11d1-BB3C-00A0C913CAD4}"},
  67. {REDIRECTOR_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F65-9E4D-11d1-BB3C-00A0C913CAD4}"},
  68. {SERVER_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F66-9E4D-11d1-BB3C-00A0C913CAD4}"},
  69. {SERVER_QUEUE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F67-9E4D-11d1-BB3C-00A0C913CAD4}"},
  70. {PAGEFILE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F68-9E4D-11d1-BB3C-00A0C913CAD4}"},
  71. {BROWSER_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F69-9E4D-11d1-BB3C-00A0C913CAD4}"},
  72. {EXPROCESS_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6A-9E4D-11d1-BB3C-00A0C913CAD4}"},
  73. {IMAGE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6B-9E4D-11d1-BB3C-00A0C913CAD4}"},
  74. {THREAD_DETAILS_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6C-9E4D-11d1-BB3C-00A0C913CAD4}"},
  75. {LONG_IMAGE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6D-9E4D-11d1-BB3C-00A0C913CAD4}"},
  76. {TCP_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6E-9E4D-11d1-BB3C-00A0C913CAD4}"},
  77. {UDP_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F6F-9E4D-11d1-BB3C-00A0C913CAD4}"},
  78. {IP_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F70-9E4D-11d1-BB3C-00A0C913CAD4}"},
  79. {ICMP_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F71-9E4D-11d1-BB3C-00A0C913CAD4}"},
  80. {NET_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F72-9E4D-11d1-BB3C-00A0C913CAD4}"},
  81. {NBT_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F73-9E4D-11d1-BB3C-00A0C913CAD4}"},
  82. {NBF_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F74-9E4D-11d1-BB3C-00A0C913CAD4}"},
  83. {NBF_RESOURCE_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F75-9E4D-11d1-BB3C-00A0C913CAD4}"},
  84. {FTP_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F76-9E4D-11d1-BB3C-00A0C913CAD4}"},
  85. {RAS_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F77-9E4D-11d1-BB3C-00A0C913CAD4}"},
  86. {WIN_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F78-9E4D-11d1-BB3C-00A0C913CAD4}"},
  87. {SFM_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F79-9E4D-11d1-BB3C-00A0C913CAD4}"},
  88. {ATK_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F7A-9E4D-11d1-BB3C-00A0C913CAD4}"},
  89. {BH_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F7B-9E4D-11d1-BB3C-00A0C913CAD4}"},
  90. {TAPI_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F7C-9E4D-11d1-BB3C-00A0C913CAD4}"},
  91. {LSPL_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F7D-9E4D-11d1-BB3C-00A0C913CAD4}"},
  92. {JOB_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F7E-9E4D-11d1-BB3C-00A0C913CAD4}"},
  93. {JOB_DETAILS_OBJECT_TITLE_INDEX, (LPWSTR)L"{5C7A4F7F-9E4D-11d1-BB3C-00A0C913CAD4}"}
  94. // {RSVP_FIRST_COUNTER_INDEX, (LPWSTR)L"{5C7A4F80-9E4D-11d1-BB3C-00A0C913CAD4}"}
  95. };
  96. const PPERFOBJECT_LOOKUP PerfObjectGuids = (const PPERFOBJECT_LOOKUP)&PerfObjectGuidTable[0];
  97. const DWORD dwNumPerfObjectGuids = (sizeof(PerfObjectGuidTable) / sizeof(PerfObjectGuidTable[0]));
  98. LPCWSTR szRawClass = (LPCWSTR)L"Win32_PerfRawData";
  99. LPCWSTR szFmtClass = (LPCWSTR)L"Win32_PerfFormattedData";
  100. LPCWSTR szGenericProviderName = (LPCWSTR)L"NT5_GenericPerfProvider_V1";
  101. LPCWSTR szGenericProviderGuid = (LPCWSTR)L"{FF37A93C-C28E-11D1-AEB6-00C04FB68820}";
  102. __inline
  103. static
  104. PPERF_OBJECT_TYPE
  105. FirstObject (
  106. PPERF_DATA_BLOCK pPerfData
  107. )
  108. {
  109. return ((PPERF_OBJECT_TYPE) ((PBYTE) pPerfData + pPerfData->HeaderLength));
  110. }
  111. __inline
  112. static
  113. PPERF_OBJECT_TYPE
  114. NextObject (
  115. PPERF_OBJECT_TYPE pObject
  116. )
  117. { // NextObject
  118. return ((PPERF_OBJECT_TYPE) ((PBYTE) pObject + pObject->TotalByteLength));
  119. } // NextObject
  120. __inline
  121. static
  122. PERF_COUNTER_DEFINITION *
  123. FirstCounter(
  124. PERF_OBJECT_TYPE *pObjectDef
  125. )
  126. {
  127. return (PERF_COUNTER_DEFINITION *)
  128. ((PCHAR) pObjectDef + pObjectDef->HeaderLength);
  129. }
  130. __inline
  131. static
  132. PERF_COUNTER_DEFINITION *
  133. NextCounter(
  134. PERF_COUNTER_DEFINITION *pCounterDef
  135. )
  136. {
  137. return (PERF_COUNTER_DEFINITION *)
  138. ((PCHAR) pCounterDef + pCounterDef->ByteLength);
  139. }
  140. __inline
  141. static
  142. PERF_INSTANCE_DEFINITION *
  143. FirstInstance(
  144. PERF_OBJECT_TYPE * pObjectDef)
  145. {
  146. return (PERF_INSTANCE_DEFINITION * )
  147. ((PCHAR) pObjectDef + pObjectDef->DefinitionLength);
  148. }
  149. __inline
  150. static
  151. PERF_INSTANCE_DEFINITION *
  152. NextInstance(
  153. PERF_INSTANCE_DEFINITION * pInstDef)
  154. {
  155. PERF_COUNTER_BLOCK *pCounterBlock;
  156. pCounterBlock = (PERF_COUNTER_BLOCK *)
  157. ((PCHAR) pInstDef + pInstDef->ByteLength);
  158. return (PERF_INSTANCE_DEFINITION * )
  159. ((PCHAR) pCounterBlock + pCounterBlock->ByteLength);
  160. }
  161. __inline
  162. static
  163. LPCWSTR
  164. GetInstanceName(
  165. PERF_INSTANCE_DEFINITION *pInstDef
  166. )
  167. {
  168. static WCHAR szLocalName[MAX_PATH];
  169. LPWSTR szSrc, szDest;
  170. assert ((pInstDef->NameLength) < (MAX_PATH * sizeof(WCHAR)));
  171. szDest = &szLocalName[0];
  172. szSrc = (LPWSTR) ((PCHAR) pInstDef + pInstDef->NameOffset);
  173. while (*szSrc != 0) {
  174. switch (*szSrc) {
  175. case '\\':
  176. *szDest++ = *szSrc;
  177. *szDest++ = *szSrc++;
  178. break;
  179. default:
  180. *szDest++ = *szSrc++;
  181. };
  182. }
  183. *szDest++ = 0;
  184. return (LPCWSTR)&szLocalName[0];
  185. }
  186. static
  187. __inline
  188. DWORD
  189. AddStringToBuffer (
  190. LPWSTR szBuffer,
  191. LPWSTR szNewString,
  192. LPDWORD pLength,
  193. LPDWORD pTotalLength,
  194. DWORD dwMaxLength
  195. )
  196. {
  197. DWORD dwReturn = ERROR_SUCCESS;
  198. if ((*pTotalLength + *pLength) < dwMaxLength) {
  199. memcpy (&szBuffer[*pTotalLength], szNewString, (*pLength * sizeof(WCHAR)));
  200. *pTotalLength += *pLength;
  201. szBuffer[*pTotalLength] = 0;
  202. } else {
  203. *pTotalLength += *pLength;
  204. dwReturn = ERROR_INSUFFICIENT_BUFFER;
  205. }
  206. return dwReturn;
  207. }
  208. LPCWSTR
  209. FormatPerfName (
  210. LPWSTR szNameIn,
  211. BOOL bHiddenCounter
  212. )
  213. {
  214. static WCHAR szStringBuffer[MAX_PATH];
  215. LPWSTR szSrc, szDest;
  216. BOOL bUpCase = FALSE;
  217. memset(szStringBuffer, 0, sizeof(szStringBuffer));
  218. szDest = &szStringBuffer[0];
  219. if (szNameIn != NULL) {
  220. for (szSrc = szNameIn; *szSrc != 0; szSrc++) {
  221. switch (*szSrc) {
  222. case '%':
  223. lstrcpyW(szDest, (LPCWSTR)L"Percent");
  224. szDest += lstrlenW((LPCWSTR)L"Percent");
  225. bUpCase = TRUE;
  226. break;
  227. case '#':
  228. lstrcpyW(szDest, (LPCWSTR)L"NumberOf");
  229. szDest += lstrlenW((LPCWSTR)L"NumberOf");
  230. bUpCase = TRUE;
  231. break;
  232. case '/':
  233. lstrcpyW(szDest, (LPCWSTR)L"Per");
  234. szDest += lstrlenW((LPCWSTR)L"Per");
  235. bUpCase = TRUE;
  236. break;
  237. case ' ':
  238. case ')':
  239. case '(':
  240. case '.':
  241. case '-':
  242. // skip
  243. bUpCase = TRUE;
  244. break;
  245. default:
  246. if (bUpCase) {
  247. *szDest++ = towupper(*szSrc);
  248. bUpCase = FALSE;
  249. } else {
  250. *szDest++ = *szSrc;
  251. }
  252. break;
  253. }
  254. }
  255. if (bHiddenCounter) {
  256. lstrcpyW (szDest, (LPCWSTR)L"_Base");
  257. szDest += lstrlenW((LPCWSTR)L"_Base");
  258. }
  259. }
  260. *szDest = 0;
  261. return (LPCWSTR)&szStringBuffer[0];
  262. }
  263. PERFTYPE_LOOKUP *
  264. GetPerfTypeInfo (
  265. DWORD dwType
  266. )
  267. {
  268. DWORD dwIndex = 0;
  269. while (dwIndex < dwNumPerfTypes) {
  270. if (dwType == (DWORD)PerfTypes[dwIndex].PerfType) {
  271. return (&PerfTypes[dwIndex]);
  272. } else {
  273. dwIndex++;
  274. }
  275. }
  276. return NULL;
  277. }
  278. LPCWSTR
  279. GetPerfObjectGuid (
  280. DWORD dwObjectId
  281. )
  282. {
  283. DWORD dwIndex = 0;
  284. while (dwIndex < dwNumPerfObjectGuids) {
  285. if (dwObjectId == (DWORD)PerfObjectGuids[dwIndex].PerfObjectId) {
  286. return ((LPCWSTR)PerfObjectGuids[dwIndex].GuidString);
  287. } else {
  288. dwIndex++;
  289. }
  290. }
  291. return (LPCWSTR)L"";
  292. }
  293. DWORD
  294. GenerateMofHeader (
  295. LPWSTR szBuffer, // string buffer
  296. LPCWSTR szComputerName,
  297. LPDWORD pcchBufferSize // max size in characters in, size used out
  298. )
  299. {
  300. WCHAR szMachineName[MAX_PATH];
  301. WCHAR szTempBuffer[MAX_PATH];
  302. DWORD dwTotalLength = 0;
  303. DWORD dwLength = sizeof(szMachineName)/sizeof(szMachineName[0]);
  304. SYSTEMTIME st;
  305. DWORD dwReturn = ERROR_SUCCESS;
  306. if (szComputerName == NULL) {
  307. GetComputerNameW(&szMachineName[0], &dwLength);
  308. } else if (szComputerName[0] == 0){
  309. GetComputerNameW(&szMachineName[0], &dwLength);
  310. } else {
  311. lstrcpyW (szMachineName, szComputerName);
  312. }
  313. GetLocalTime( &st );
  314. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A//WBEM Performance Data MOF Dumped from machine %ws on %2.2d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d\x0D\x0A",
  315. szMachineName,
  316. st.wMonth, st.wDay, (st.wYear % 100),
  317. st.wHour, st.wMinute, st.wSecond);
  318. dwReturn = AddStringToBuffer (
  319. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  320. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A#pragma autorecover");
  321. dwReturn = AddStringToBuffer (
  322. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  323. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A#pragma namespace (\"\\\\\\\\.\\\\Root\\\\Default\")");
  324. dwReturn = AddStringToBuffer (
  325. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  326. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A#pragma classflags(\"forceupdate\")\x0D\x0A");
  327. dwReturn = AddStringToBuffer (
  328. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  329. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier vendor:ToInstance;");
  330. dwReturn = AddStringToBuffer (
  331. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  332. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier classguid:ToInstance;");
  333. dwReturn = AddStringToBuffer (
  334. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  335. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier locale:ToInstance;");
  336. dwReturn = AddStringToBuffer (
  337. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  338. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier display:ToInstance;");
  339. dwReturn = AddStringToBuffer (
  340. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  341. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier perfindex:ToInstance;");
  342. dwReturn = AddStringToBuffer (
  343. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  344. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier helpindex:ToInstance;");
  345. dwReturn = AddStringToBuffer (
  346. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  347. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier perfdetail:ToInstance;");
  348. dwReturn = AddStringToBuffer (
  349. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  350. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier countertype:ToInstance;");
  351. dwReturn = AddStringToBuffer (
  352. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  353. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier perfdefault:ToInstance;");
  354. dwReturn = AddStringToBuffer (
  355. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  356. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0Aqualifier defaultscale:ToInstance;");
  357. dwReturn = AddStringToBuffer (
  358. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  359. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A");
  360. dwReturn = AddStringToBuffer (
  361. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  362. *pcchBufferSize = dwTotalLength;
  363. return dwReturn;
  364. }
  365. DWORD
  366. GenerateMofObject (
  367. LPWSTR szBuffer,
  368. LPDWORD pcchBufferSize,
  369. PPERF_COUNTER_DLL_INFO pPcDllInfo,
  370. PERF_OBJECT_TYPE *pPerfObject,
  371. LPWSTR *lpCounterText, // name strings array
  372. LPWSTR *lpDisplayText, // Localized name strings array
  373. DWORD dwFlags
  374. )
  375. {
  376. BOOL bRawDefinition = (dwFlags & WM_GMO_RAW_DEFINITION);
  377. BOOL bCostlyObject = (dwFlags & WM_GMO_COSTLY_OBJECT);
  378. BOOL bDefaultObject = (dwFlags & WM_GMO_DEFAULT_OBJECT);
  379. WCHAR szTempBuffer[MAX_PATH];
  380. DWORD dwTotalLength = 0;
  381. DWORD dwLength;
  382. DWORD dwReturn = ERROR_SUCCESS;
  383. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A\x0D\x0A[");
  384. dwReturn = AddStringToBuffer (
  385. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  386. if (pPerfObject->NumInstances == PERF_NO_INSTANCES) {
  387. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A singleton,");
  388. dwReturn = AddStringToBuffer (
  389. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  390. }
  391. if (bCostlyObject) {
  392. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A costly,");
  393. dwReturn = AddStringToBuffer (
  394. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  395. }
  396. if (bDefaultObject) {
  397. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfdefault,");
  398. dwReturn = AddStringToBuffer (
  399. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  400. }
  401. if (pPcDllInfo != NULL) {
  402. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A dynamic,");
  403. dwReturn = AddStringToBuffer (
  404. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  405. if ((dwReturn == ERROR_SUCCESS) && (pPcDllInfo->szWbemProviderName != NULL)) {
  406. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A provider(\"%s\"),",
  407. szGenericProviderName);
  408. dwReturn = AddStringToBuffer (
  409. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  410. } else {
  411. // if this structure is used, all fields must be present
  412. dwReturn = ERROR_INVALID_PARAMETER;
  413. }
  414. if ((dwReturn == ERROR_SUCCESS) && (pPcDllInfo->szRegistryKey != NULL)) {
  415. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A registrykey(\"%s\"),",
  416. pPcDllInfo->szRegistryKey);
  417. dwReturn = AddStringToBuffer (
  418. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  419. } else {
  420. // if this structure is used, all fields must be present
  421. dwReturn = ERROR_INVALID_PARAMETER;
  422. }
  423. if ((dwReturn == ERROR_SUCCESS) && (pPcDllInfo->szClassGuid != NULL)) {
  424. if (pPcDllInfo->szClassGuid[0] != 0) {
  425. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A classguid(\"%s\"),",
  426. pPcDllInfo->szClassGuid);
  427. dwReturn = AddStringToBuffer (
  428. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  429. }
  430. } else {
  431. // if this structure is used, all fields must be present
  432. dwReturn = ERROR_INVALID_PARAMETER;
  433. }
  434. }
  435. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A locale(\"0x%4.4x\"),", (GetSystemDefaultLCID() & 0x0000FFFF));
  436. dwReturn = AddStringToBuffer (
  437. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  438. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A display(\"%ws\"),",
  439. lpDisplayText[pPerfObject->ObjectNameTitleIndex]);
  440. dwReturn = AddStringToBuffer (
  441. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  442. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfindex(%d),", pPerfObject->ObjectNameTitleIndex);
  443. dwReturn = AddStringToBuffer (
  444. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  445. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A helpindex(%d),", pPerfObject->ObjectHelpTitleIndex);
  446. dwReturn = AddStringToBuffer (
  447. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  448. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfdetail(%d)", pPerfObject->DetailLevel);
  449. dwReturn = AddStringToBuffer (
  450. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  451. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A]%sclass Win32Perf_%ws%ws : %ws\x0D\x0A{",
  452. (LPCWSTR)L"\x0D\x0A", (bRawDefinition ? (LPCWSTR)L"Raw" : (LPCWSTR)L""),
  453. FormatPerfName(lpCounterText[pPerfObject->ObjectNameTitleIndex],FALSE),
  454. (bRawDefinition ? szRawClass : szFmtClass));
  455. dwReturn = AddStringToBuffer (
  456. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  457. // add an entry for the instance name here if the object has instances
  458. if (pPerfObject->NumInstances != PERF_NO_INSTANCES) {
  459. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A [key]\x0D\x0A string\tName;");
  460. dwReturn = AddStringToBuffer (
  461. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  462. }
  463. *pcchBufferSize = dwTotalLength;
  464. return dwReturn;
  465. }
  466. DWORD
  467. GenerateMofObjectTail (
  468. LPWSTR szBuffer,
  469. LPDWORD pcchBufferSize
  470. )
  471. {
  472. WCHAR szTempBuffer[MAX_PATH];
  473. DWORD dwTotalLength = 0;
  474. DWORD dwLength;
  475. DWORD dwReturn = ERROR_SUCCESS;
  476. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A};\x0D\x0A");
  477. dwReturn = AddStringToBuffer (
  478. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  479. *pcchBufferSize = dwTotalLength;
  480. return dwReturn;
  481. }
  482. DWORD
  483. GenerateMofCounter (
  484. LPWSTR szBuffer,
  485. LPDWORD pcchBufferSize,
  486. PERF_COUNTER_DEFINITION *pPerfCounter,
  487. LPWSTR *lpCounterText, // name strings array
  488. LPWSTR *lpDisplayText, // Localized name strings array
  489. DWORD dwFlags
  490. )
  491. {
  492. WCHAR szTempBuffer[MAX_PATH];
  493. DWORD dwTotalLength = 0;
  494. DWORD dwLength;
  495. DWORD dwReturn = ERROR_SUCCESS;
  496. BOOL bRawDefinition = (dwFlags & WM_GMO_RAW_DEFINITION);
  497. BOOL bDefaultCounter = (dwFlags & WM_GMO_DEFAULT_COUNTER);
  498. PERFTYPE_LOOKUP *pType;
  499. pType = GetPerfTypeInfo (pPerfCounter->CounterType);
  500. if (pType != NULL) {
  501. if (bRawDefinition || IsDisplayableType(pPerfCounter->CounterType)) {
  502. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A [");
  503. dwReturn = AddStringToBuffer (
  504. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  505. if (bDefaultCounter) {
  506. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfdefault,");
  507. dwReturn = AddStringToBuffer (
  508. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  509. }
  510. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A display(\"%ws\"),",
  511. lpDisplayText[pPerfCounter->CounterNameTitleIndex]);
  512. dwReturn = AddStringToBuffer (
  513. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  514. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A countertype(%u),", pPerfCounter->CounterType);
  515. dwReturn = AddStringToBuffer (
  516. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  517. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfindex(%d),", pPerfCounter->CounterNameTitleIndex);
  518. dwReturn = AddStringToBuffer (
  519. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  520. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A helpindex(%d),", pPerfCounter->CounterHelpTitleIndex);
  521. dwReturn = AddStringToBuffer (
  522. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  523. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A defaultscale(%d),", pPerfCounter->DefaultScale);
  524. dwReturn = AddStringToBuffer (
  525. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  526. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A perfdetail(%d)", pPerfCounter->DetailLevel);
  527. dwReturn = AddStringToBuffer (
  528. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  529. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A ]\x0D\x0A %ws\t%ws;\x0D\x0A",
  530. (bRawDefinition ? pType->RawType : pType->FmtType),
  531. FormatPerfName (lpCounterText[pPerfCounter->CounterNameTitleIndex],
  532. (IsDisplayableType(pPerfCounter->CounterType) ? FALSE : TRUE)));
  533. dwReturn = AddStringToBuffer (
  534. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  535. // return size used
  536. *pcchBufferSize = dwTotalLength;
  537. }
  538. } else {
  539. dwReturn = ERROR_FILE_NOT_FOUND;
  540. }
  541. return dwReturn;
  542. }
  543. DWORD
  544. GenerateMofInstances (
  545. LPWSTR szBuffer,
  546. LPDWORD pcchBufferSize,
  547. PERF_DATA_BLOCK * pPerfDataBlock,
  548. PERF_OBJECT_TYPE * pPerfObject,
  549. LPWSTR * lpCounterText, // name strings array
  550. LPWSTR * lpDisplayText, // Localized name strings array
  551. DWORD dwFlags
  552. )
  553. {
  554. WCHAR szTempBuffer[MAX_PATH];
  555. DWORD dwTotalLength = 0;
  556. DWORD dwLength;
  557. DWORD dwReturn = ERROR_SUCCESS;
  558. BOOL bRawDefinition = (dwFlags & WM_GMO_RAW_DEFINITION);
  559. //
  560. // for each instance, dump all the counter data for that instance
  561. //
  562. PERF_INSTANCE_DEFINITION * pThisInstance;
  563. PERF_INSTANCE_DEFINITION * pParentInstance;
  564. PERF_OBJECT_TYPE * pParentObject;
  565. PERF_COUNTER_DEFINITION * pThisCounter;
  566. DWORD dwParentInstIdx;
  567. DWORD dwObjectIdx;
  568. DWORD dwInstanceIndex;
  569. DWORD dwCounterCount;
  570. LPDWORD pValue;
  571. LONGLONG llTimeStamp;
  572. UNREFERENCED_PARAMETER (lpDisplayText);
  573. if (pPerfObject->NumInstances == PERF_NO_INSTANCES) {
  574. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A\x0D\x0Ainstance of Win32Perf_%ws%ws\x0D\x0A{",
  575. (bRawDefinition ? (LPCWSTR)L"Raw" : (LPCWSTR)L""),
  576. FormatPerfName(lpCounterText[pPerfObject->ObjectNameTitleIndex], FALSE));
  577. dwReturn = AddStringToBuffer (
  578. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  579. GetSystemTimeAsFileTime ((LPFILETIME)&llTimeStamp);
  580. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A Timestamp = ");
  581. dwReturn = AddStringToBuffer (
  582. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  583. pValue = (LPDWORD)&llTimeStamp;
  584. pValue++;
  585. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"0x%8.8x", *pValue);
  586. dwReturn = AddStringToBuffer (
  587. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  588. pValue--;
  589. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"%8.8x;", *pValue);
  590. dwReturn = AddStringToBuffer (
  591. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  592. pThisCounter = FirstCounter(pPerfObject);
  593. assert (pThisCounter != NULL);
  594. for (dwCounterCount = 0; dwCounterCount < pPerfObject->NumCounters; dwCounterCount++) {
  595. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A %ws = ",
  596. FormatPerfName (lpCounterText[pThisCounter->CounterNameTitleIndex],
  597. (IsDisplayableType(pThisCounter->CounterType) ? FALSE : TRUE)));
  598. dwReturn = AddStringToBuffer (
  599. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  600. pValue = (LPDWORD)pPerfObject;
  601. pValue = (LPDWORD)((LPBYTE)pValue + pPerfObject->DefinitionLength);
  602. pValue = (LPDWORD)((LPBYTE)pValue + pThisCounter->CounterOffset);
  603. if (pThisCounter->CounterSize == 8) {
  604. pValue++;
  605. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"0x%8.8x", *pValue);
  606. dwReturn = AddStringToBuffer (
  607. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  608. pValue--;
  609. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"%8.8x;", *pValue);
  610. dwReturn = AddStringToBuffer (
  611. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  612. } else {
  613. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"0x%8.8x;", *pValue);
  614. dwReturn = AddStringToBuffer (
  615. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  616. }
  617. pThisCounter = NextCounter (pThisCounter);
  618. }
  619. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A};\x0D\x0A");
  620. dwReturn = AddStringToBuffer (
  621. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  622. } else {
  623. // do the multiple instance case here
  624. pThisInstance = FirstInstance(pPerfObject);
  625. for (dwInstanceIndex = 0; dwInstanceIndex < (DWORD)pPerfObject->NumInstances; dwInstanceIndex++) {
  626. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A\x0D\x0Ainstance of Win32Perf_%ws%ws\x0D\x0A{",
  627. (bRawDefinition ? (LPCWSTR)L"Raw" : (LPCWSTR)L""),
  628. FormatPerfName(lpCounterText[pPerfObject->ObjectNameTitleIndex],FALSE));
  629. dwReturn = AddStringToBuffer (
  630. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  631. if (pThisInstance->ParentObjectTitleIndex > 0) {
  632. // get parent instance name
  633. pParentObject = FirstObject(pPerfDataBlock);
  634. dwObjectIdx = 0;
  635. while ((pParentObject != NULL) &&
  636. (pParentObject->ObjectNameTitleIndex != pThisInstance->ParentObjectTitleIndex) &&
  637. (dwObjectIdx < pPerfDataBlock->NumObjectTypes)) {
  638. pParentObject = NextObject (pParentObject);
  639. dwObjectIdx++;
  640. }
  641. if (pParentObject->ObjectNameTitleIndex == pThisInstance->ParentObjectTitleIndex) {
  642. pParentInstance = FirstInstance (pParentObject);
  643. for (dwParentInstIdx = 0;
  644. dwParentInstIdx < pThisInstance->ParentObjectInstance;
  645. dwParentInstIdx++) {
  646. pParentInstance = NextInstance(pParentInstance);
  647. }
  648. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A Name = \"%ws/",
  649. GetInstanceName (pParentInstance));
  650. dwReturn = AddStringToBuffer (
  651. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  652. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"%ws\";", GetInstanceName (pThisInstance));
  653. dwReturn = AddStringToBuffer (
  654. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  655. } else {
  656. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A Name = \"%ws\";",
  657. GetInstanceName (pThisInstance));
  658. dwReturn = AddStringToBuffer (
  659. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  660. }
  661. } else {
  662. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A Name = \"%ws\";",
  663. GetInstanceName (pThisInstance));
  664. dwReturn = AddStringToBuffer (
  665. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  666. }
  667. pThisCounter = FirstCounter(pPerfObject);
  668. assert (pThisCounter != NULL);
  669. for (dwCounterCount = 0; dwCounterCount < pPerfObject->NumCounters; dwCounterCount++) {
  670. assert (pThisCounter != NULL);
  671. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A %ws = ",
  672. FormatPerfName (lpCounterText[pThisCounter->CounterNameTitleIndex],
  673. (IsDisplayableType(pThisCounter->CounterType) ? FALSE : TRUE)));
  674. dwReturn = AddStringToBuffer (
  675. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  676. pValue = (LPDWORD)pThisInstance;
  677. pValue = (LPDWORD)((LPBYTE)pValue + pThisInstance->ByteLength);
  678. pValue = (LPDWORD)((LPBYTE)pValue + pThisCounter->CounterOffset);
  679. if (pThisCounter->CounterSize == 8) {
  680. pValue++;
  681. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"0x%8.8x", *pValue);
  682. dwReturn = AddStringToBuffer (
  683. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  684. pValue--;
  685. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"%8.8x;", *pValue);
  686. dwReturn = AddStringToBuffer (
  687. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  688. } else {
  689. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"0x%8.8x;", *pValue);
  690. dwReturn = AddStringToBuffer (
  691. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  692. }
  693. pThisCounter = NextCounter (pThisCounter);
  694. }
  695. dwLength = swprintf (szTempBuffer, (LPCWSTR)L"\x0D\x0A};\x0D\x0A");
  696. dwReturn = AddStringToBuffer (
  697. szBuffer, szTempBuffer, &dwLength, &dwTotalLength, *pcchBufferSize);
  698. pThisInstance = NextInstance(pThisInstance);
  699. }
  700. }
  701. *pcchBufferSize = dwTotalLength;
  702. return dwReturn;
  703. }