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.

480 lines
15 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. perfname.c
  5. Abstract:
  6. <abstract>
  7. --*/
  8. #include <windows.h>
  9. #include <winperf.h>
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <tchar.h>
  15. #include <mbctype.h>
  16. #include <pdh.h>
  17. #include "pdhitype.h"
  18. #include "pdhidef.h"
  19. #include "pdhmsg.h"
  20. #include "strings.h"
  21. LPCWSTR
  22. PdhiLookupPerfNameByIndex (
  23. PPERF_MACHINE pMachine,
  24. DWORD dwNameIndex
  25. )
  26. {
  27. LPWSTR szReturn = NULL;
  28. LONG lStatus = ERROR_SUCCESS;
  29. SetLastError (lStatus);
  30. if (pMachine != NULL) {
  31. if (pMachine->dwStatus == ERROR_SUCCESS) {
  32. if (dwNameIndex <= pMachine->dwLastPerfString) {
  33. szReturn = pMachine->szPerfStrings[dwNameIndex];
  34. } else {
  35. lStatus = PDH_INVALID_ARGUMENT;
  36. }
  37. } else {
  38. lStatus = pMachine->dwStatus ;
  39. }
  40. } else {
  41. lStatus = PDH_CSTATUS_NO_MACHINE;
  42. }
  43. SetLastError (lStatus);
  44. return (LPCWSTR)szReturn;
  45. }
  46. DWORD
  47. PdhiLookupPerfIndexByName (
  48. PPERF_MACHINE pMachine,
  49. LPCWSTR szNameBuffer
  50. )
  51. {
  52. DWORD dwCurrentIndex = 2;
  53. BOOL bDone = FALSE;
  54. LPWSTR szThisPerfString;
  55. SetLastError (ERROR_SUCCESS);
  56. while (!bDone) {
  57. // test even indices first
  58. for (;
  59. dwCurrentIndex <= pMachine->dwLastPerfString;
  60. dwCurrentIndex += 2) {
  61. szThisPerfString = pMachine->szPerfStrings[dwCurrentIndex];
  62. if (szThisPerfString != NULL) {
  63. if (lstrcmpiW(szNameBuffer, szThisPerfString) == 0) {
  64. // match found
  65. bDone = TRUE;
  66. break;
  67. }
  68. }
  69. }
  70. if (!bDone) {
  71. // if doing an odd # & not done then exit because we've
  72. // looked at them all and not found anything
  73. if (dwCurrentIndex & 0x00000001) break;
  74. dwCurrentIndex = 3;
  75. } // else just go to the loop exit
  76. }
  77. if (!bDone) {
  78. SetLastError (PDH_STRING_NOT_FOUND);
  79. dwCurrentIndex = 0;
  80. }
  81. return dwCurrentIndex;
  82. }
  83. PDH_FUNCTION
  84. PdhLookupPerfNameByIndexW (
  85. LPCWSTR szMachineName,
  86. DWORD dwNameIndex,
  87. LPWSTR szNameBuffer,
  88. LPDWORD pcchNameBufferSize
  89. )
  90. {
  91. PPERF_MACHINE pMachine;
  92. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  93. LPWSTR szLocalMachineName = NULL;
  94. LPWSTR szLocalName;
  95. DWORD dwNameLen;
  96. DWORD dwLocalNameSize = 0;
  97. if ((szNameBuffer == NULL) ||
  98. (pcchNameBufferSize == NULL)) {
  99. pdhStatus = PDH_INVALID_ARGUMENT;
  100. } else {
  101. // test access to all the parameteres passed in before continuing
  102. __try {
  103. if (szMachineName == NULL) {
  104. // use local machine name
  105. szLocalMachineName = &szStaticLocalMachineName[0];
  106. } else {
  107. if (*szMachineName == 0) {
  108. // NULL machine name
  109. pdhStatus = PDH_INVALID_ARGUMENT;
  110. } else {
  111. szLocalMachineName = (LPWSTR)szMachineName;
  112. }
  113. }
  114. if (pdhStatus == ERROR_SUCCESS) {
  115. dwLocalNameSize = *pcchNameBufferSize;
  116. if (dwLocalNameSize >= sizeof(DWORD)) {
  117. //test write access to the beginning and the end of the buffer
  118. CLEAR_FIRST_FOUR_BYTES (szNameBuffer);
  119. szNameBuffer[dwLocalNameSize -1] = 0;
  120. } else if (dwLocalNameSize >= sizeof (WORD)) {
  121. // then just test the first char
  122. *szNameBuffer = 0;
  123. } else {
  124. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  125. }
  126. }
  127. } __except (EXCEPTION_EXECUTE_HANDLER) {
  128. pdhStatus = PDH_INVALID_ARGUMENT;
  129. }
  130. }
  131. if (pdhStatus == ERROR_SUCCESS) {
  132. pMachine = GetMachine (szLocalMachineName, 0);
  133. if (pMachine != NULL) {
  134. if (pMachine->dwStatus == ERROR_SUCCESS) {
  135. szLocalName = (LPWSTR)PdhiLookupPerfNameByIndex (
  136. pMachine, dwNameIndex);
  137. if (szLocalName != NULL) {
  138. dwNameLen = lstrlenW(szLocalName) + 1;
  139. if (dwNameLen < dwLocalNameSize) {
  140. lstrcpyW (szNameBuffer, szLocalName);
  141. } else {
  142. *szNameBuffer = 0;
  143. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  144. }
  145. dwLocalNameSize = dwNameLen;
  146. } else {
  147. *szNameBuffer = 0;
  148. dwLocalNameSize = 0;
  149. pdhStatus = GetLastError();
  150. }
  151. } else {
  152. pdhStatus = pMachine->dwStatus;
  153. }
  154. pMachine->dwRefCount--;
  155. RELEASE_MUTEX (pMachine->hMutex);
  156. } else {
  157. *szNameBuffer = 0;
  158. dwLocalNameSize = 0;
  159. pdhStatus = GetLastError();
  160. }
  161. __try {
  162. *pcchNameBufferSize = dwLocalNameSize;
  163. } __except (EXCEPTION_EXECUTE_HANDLER) {
  164. pdhStatus = PDH_INVALID_ARGUMENT;
  165. }
  166. }
  167. return pdhStatus;
  168. }
  169. PDH_FUNCTION
  170. PdhLookupPerfNameByIndexA (
  171. LPCSTR szMachineName,
  172. DWORD dwNameIndex,
  173. LPSTR szNameBuffer,
  174. LPDWORD pcchNameBufferSize
  175. )
  176. {
  177. PPERF_MACHINE pMachine;
  178. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  179. LPWSTR szLocalMachineName = NULL;
  180. LPWSTR szLocalName;
  181. DWORD dwNameLen;
  182. DWORD dwLocalNameSize = 0;
  183. if ((szNameBuffer == NULL) ||
  184. (pcchNameBufferSize == NULL)) {
  185. pdhStatus = PDH_INVALID_ARGUMENT;
  186. } else {
  187. // test access to all the parameteres passed in before continuing
  188. __try {
  189. if (szMachineName == NULL) {
  190. // use local machine name
  191. szLocalMachineName = &szStaticLocalMachineName[0];
  192. } else {
  193. if (*szMachineName == 0) {
  194. // NULL machine name
  195. pdhStatus = PDH_INVALID_ARGUMENT;
  196. } else {
  197. // then allocate a new buffer and convert the LPSTR to a LPWSTR
  198. dwNameLen = lstrlenA(szMachineName) + 1;
  199. szLocalMachineName = G_ALLOC (dwNameLen * sizeof(WCHAR));
  200. if (szLocalMachineName != NULL) {
  201. MultiByteToWideChar(_getmbcp(),
  202. 0,
  203. szMachineName,
  204. lstrlenA(szMachineName),
  205. (LPWSTR) szLocalMachineName,
  206. dwNameLen);
  207. } else {
  208. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  209. }
  210. }
  211. }
  212. if (pdhStatus == ERROR_SUCCESS) {
  213. dwLocalNameSize = *pcchNameBufferSize;
  214. if (dwLocalNameSize >= sizeof(DWORD)) {
  215. //test write access to the beginning and the end of the buffer
  216. CLEAR_FIRST_FOUR_BYTES (szNameBuffer);
  217. szNameBuffer[dwLocalNameSize -1] = 0;
  218. } else if (dwLocalNameSize >= sizeof (CHAR)) {
  219. // then just test the first char
  220. *szNameBuffer = 0;
  221. } else {
  222. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  223. }
  224. }
  225. } __except (EXCEPTION_EXECUTE_HANDLER) {
  226. pdhStatus = PDH_INVALID_ARGUMENT;
  227. }
  228. }
  229. if (pdhStatus == ERROR_SUCCESS) {
  230. pMachine = GetMachine (szLocalMachineName, 0);
  231. if (pMachine != NULL) {
  232. if (pMachine->dwStatus == ERROR_SUCCESS) {
  233. szLocalName = (LPWSTR)PdhiLookupPerfNameByIndex (
  234. pMachine, dwNameIndex);
  235. if (szLocalName != NULL) {
  236. pdhStatus = PdhiConvertUnicodeToAnsi(_getmbcp(),
  237. szLocalName, szNameBuffer, & dwLocalNameSize);
  238. } else {
  239. *szNameBuffer = 0;
  240. dwLocalNameSize = 0;
  241. pdhStatus = GetLastError();
  242. }
  243. } else {
  244. pdhStatus = pMachine->dwStatus;
  245. }
  246. pMachine->dwRefCount--;
  247. RELEASE_MUTEX (pMachine->hMutex);
  248. } else {
  249. *szNameBuffer = 0;
  250. dwLocalNameSize = 0;
  251. pdhStatus = GetLastError();
  252. }
  253. __try {
  254. *pcchNameBufferSize = dwLocalNameSize;
  255. } __except (EXCEPTION_EXECUTE_HANDLER) {
  256. pdhStatus = PDH_INVALID_ARGUMENT;
  257. }
  258. }
  259. if (szLocalMachineName != NULL) {
  260. if (szLocalMachineName != &szStaticLocalMachineName[0]) {
  261. G_FREE (szLocalMachineName);
  262. }
  263. }
  264. return pdhStatus;
  265. }
  266. PDH_FUNCTION
  267. PdhLookupPerfIndexByNameW (
  268. LPCWSTR szMachineName,
  269. LPCWSTR szNameBuffer,
  270. LPDWORD pdwIndex
  271. )
  272. {
  273. PPERF_MACHINE pMachine;
  274. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  275. LPWSTR szLocalMachineName = NULL;
  276. DWORD dwIndexFound;
  277. if ((szNameBuffer == NULL) ||
  278. (pdwIndex == NULL)) {
  279. pdhStatus = PDH_INVALID_ARGUMENT;
  280. } else {
  281. // test access to all the parameteres passed in before continuing
  282. __try {
  283. if (szMachineName == NULL) {
  284. // use local machine name
  285. szLocalMachineName = &szStaticLocalMachineName[0];
  286. } else {
  287. if (*szMachineName == 0) {
  288. // NULL machine name
  289. pdhStatus = PDH_INVALID_ARGUMENT;
  290. } else {
  291. szLocalMachineName = (LPWSTR)szMachineName;
  292. }
  293. }
  294. // test read access to name
  295. if (*szNameBuffer == 0) {
  296. pdhStatus = PDH_INVALID_ARGUMENT;
  297. }
  298. if (pdhStatus == ERROR_SUCCESS) {
  299. // test write access
  300. *pdwIndex = 0;
  301. }
  302. } __except (EXCEPTION_EXECUTE_HANDLER) {
  303. pdhStatus = PDH_INVALID_ARGUMENT;
  304. }
  305. }
  306. if (pdhStatus == ERROR_SUCCESS) {
  307. pMachine = GetMachine (szLocalMachineName, 0);
  308. if (pMachine != NULL) {
  309. if (pMachine->dwStatus == ERROR_SUCCESS) {
  310. dwIndexFound = PdhiLookupPerfIndexByName (
  311. pMachine, szNameBuffer);
  312. if (dwIndexFound == 0) {
  313. // match not found
  314. pdhStatus = GetLastError();
  315. } else {
  316. __try {
  317. // write value found
  318. *pdwIndex = dwIndexFound;
  319. } __except (EXCEPTION_EXECUTE_HANDLER) {
  320. pdhStatus = PDH_INVALID_ARGUMENT;
  321. }
  322. }
  323. } else {
  324. pdhStatus = pMachine->dwStatus;
  325. }
  326. pMachine->dwRefCount--;
  327. RELEASE_MUTEX (pMachine->hMutex);
  328. } else {
  329. pdhStatus = GetLastError();
  330. }
  331. }
  332. return pdhStatus;
  333. }
  334. PDH_FUNCTION
  335. PdhLookupPerfIndexByNameA (
  336. LPCSTR szMachineName,
  337. LPCSTR szNameBuffer,
  338. LPDWORD pdwIndex
  339. )
  340. {
  341. PPERF_MACHINE pMachine;
  342. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  343. LPWSTR szLocalMachineName = NULL;
  344. DWORD dwIndexFound;
  345. LPWSTR szWideName;
  346. DWORD dwNameLen;
  347. if ((szNameBuffer == NULL) ||
  348. (pdwIndex == NULL)) {
  349. pdhStatus = PDH_INVALID_ARGUMENT;
  350. } else {
  351. // test access to all the parameteres passed in before continuing
  352. __try {
  353. if (szMachineName == NULL) {
  354. // use local machine name
  355. szLocalMachineName = &szStaticLocalMachineName[0];
  356. } else {
  357. if (*szMachineName == 0) {
  358. // NULL machine name
  359. pdhStatus = PDH_INVALID_ARGUMENT;
  360. } else {
  361. // then allocate a new buffer and convert the LPSTR to a LPWSTR
  362. dwNameLen = lstrlenA(szMachineName) + 1;
  363. szLocalMachineName = G_ALLOC (dwNameLen * sizeof(WCHAR));
  364. if (szLocalMachineName != NULL) {
  365. MultiByteToWideChar(_getmbcp(),
  366. 0,
  367. szMachineName,
  368. lstrlenA(szMachineName),
  369. (LPWSTR) szLocalMachineName,
  370. dwNameLen);
  371. } else {
  372. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  373. }
  374. }
  375. }
  376. // test read access to name
  377. if (pdhStatus == ERROR_SUCCESS) {
  378. if (*szNameBuffer == 0) {
  379. pdhStatus = PDH_INVALID_ARGUMENT;
  380. }
  381. }
  382. if (pdhStatus == ERROR_SUCCESS) {
  383. // test write access
  384. *pdwIndex = 0;
  385. }
  386. } __except (EXCEPTION_EXECUTE_HANDLER) {
  387. pdhStatus = PDH_INVALID_ARGUMENT;
  388. }
  389. }
  390. if (pdhStatus == ERROR_SUCCESS) {
  391. pMachine = GetMachine (szLocalMachineName, 0);
  392. if (pMachine != NULL) {
  393. if (pMachine->dwStatus == ERROR_SUCCESS) {
  394. // convert name string to wide characters for comparison
  395. dwNameLen = lstrlenA (szNameBuffer) + 1;
  396. szWideName = G_ALLOC (dwNameLen * sizeof(WCHAR));
  397. if (szWideName != NULL) {
  398. MultiByteToWideChar(_getmbcp(),
  399. 0,
  400. szNameBuffer,
  401. lstrlenA(szNameBuffer),
  402. (LPWSTR) szWideName,
  403. dwNameLen);
  404. dwIndexFound = PdhiLookupPerfIndexByName (
  405. pMachine, szWideName);
  406. if (dwIndexFound == 0) {
  407. // match not found
  408. pdhStatus = GetLastError();
  409. } else {
  410. __try {
  411. // write value found
  412. *pdwIndex = dwIndexFound;
  413. } __except (EXCEPTION_EXECUTE_HANDLER) {
  414. pdhStatus = PDH_INVALID_ARGUMENT;
  415. }
  416. }
  417. G_FREE (szWideName);
  418. } else {
  419. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  420. }
  421. } else {
  422. pdhStatus = pMachine->dwStatus;
  423. }
  424. pMachine->dwRefCount--;
  425. RELEASE_MUTEX (pMachine->hMutex);
  426. } else {
  427. pdhStatus = GetLastError();
  428. }
  429. }
  430. if ( szLocalMachineName != NULL
  431. && szLocalMachineName != & szStaticLocalMachineName[0]) {
  432. G_FREE (szLocalMachineName);
  433. }
  434. return pdhStatus;
  435. }