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.

986 lines
28 KiB

  1. /*++
  2. Copyright (C) 1995-1999 Microsoft Corporation
  3. Module Name:
  4. vbfuncs.c
  5. Abstract:
  6. Visual Basic interface functions exposed in pdh.dll
  7. --*/
  8. #include <windows.h>
  9. #include <winperf.h>
  10. #include <pdh.h>
  11. #include "pdhidef.h"
  12. #include "pdhmsg.h"
  13. #include "strings.h"
  14. #define INITIAL_VB_LIST_SIZE (4096 * 4)
  15. #define EXTEND_VB_LIST_SIZE (4096 * 2)
  16. typedef struct _VB_STRING_LIST {
  17. LPSTR mszList; // pointer to buffer containing strings
  18. LPSTR szTermChar; // pointer to "next" char to use
  19. DWORD dwNumEntries; // number of strings
  20. DWORD dwSize; // max size (in chars) of buffer
  21. DWORD dwRemaining; // # of chars left
  22. DWORD dwLastEntryRead; // index of last string read indicating index of....
  23. DWORD dwLastItemLength; // length of last item read
  24. LPSTR szLastItemRead; // pointer to START of last item read
  25. } VB_STRING_LIST, FAR * LPVB_STRING_LIST;
  26. VB_STRING_LIST PdhivbList = {NULL, NULL, 0, 0, 0};
  27. void PdhiDialogCallBack( IN DWORD_PTR dwArg );
  28. BOOL
  29. PdhiAddStringToVbList (
  30. IN LPSTR szString
  31. );
  32. BOOL
  33. PdhiAddStringToVbList (
  34. IN LPSTR szString
  35. )
  36. {
  37. DWORD dwSize1, dwSize2;
  38. VB_STRING_LIST *pVbList;
  39. dwSize1 = lstrlen(szString) + 1;
  40. pVbList = &PdhivbList;
  41. if (dwSize1 > pVbList->dwRemaining) {
  42. dwSize2 = (DWORD)(pVbList->szTermChar - pVbList->mszList);
  43. pVbList->dwSize += EXTEND_VB_LIST_SIZE;
  44. pVbList->mszList = G_REALLOC (pVbList->mszList, pVbList->dwSize);
  45. if (pVbList->mszList == NULL) {
  46. memset(pVbList, 0, sizeof(VB_STRING_LIST));
  47. return FALSE;
  48. } else {
  49. // update values
  50. pVbList->szLastItemRead = pVbList->mszList;
  51. pVbList->szTermChar = pVbList->mszList + dwSize2;
  52. pVbList->dwRemaining += EXTEND_VB_LIST_SIZE;
  53. }
  54. }
  55. // copy new string
  56. lstrcpy (pVbList->szTermChar, szString);
  57. pVbList->dwNumEntries++;
  58. pVbList->szTermChar += dwSize1;
  59. pVbList->dwRemaining -= dwSize1;
  60. return TRUE;
  61. }
  62. void
  63. PdhiDialogCallBack(
  64. IN DWORD_PTR dwArg
  65. )
  66. {
  67. // add strings in buffer to list boxpfdh
  68. LPTSTR NewCounterName;
  69. LPTSTR NewCounterName2;
  70. LPTSTR szExpandedPath;
  71. DWORD dwSize1, dwSize2;
  72. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  73. PPDH_BROWSE_DLG_CONFIG pDlgConfig;
  74. pDlgConfig = (PPDH_BROWSE_DLG_CONFIG)dwArg;
  75. if (pDlgConfig->CallBackStatus == PDH_MORE_DATA) {
  76. // transfer buffer is too small for selection so extend it and
  77. // try again.
  78. if (pDlgConfig->szReturnPathBuffer != NULL) {
  79. G_FREE (pDlgConfig->szReturnPathBuffer);
  80. }
  81. pDlgConfig->cchReturnPathLength += EXTEND_VB_LIST_SIZE;
  82. pDlgConfig->szReturnPathBuffer =
  83. G_ALLOC ((pDlgConfig->cchReturnPathLength * sizeof (CHAR)));
  84. if (pDlgConfig->szReturnPathBuffer != NULL) {
  85. pdhStatus = PDH_RETRY;
  86. } else {
  87. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  88. }
  89. } else {
  90. for (NewCounterName = pDlgConfig->szReturnPathBuffer;
  91. (*NewCounterName != 0) && (pdhStatus == ERROR_SUCCESS);
  92. NewCounterName += (lstrlen(NewCounterName) + 1)) {
  93. if (strstr (NewCounterName, caszSplat) == NULL) {
  94. // this is a regular path entry so add it to the VB List
  95. if (!PdhiAddStringToVbList (NewCounterName)) {
  96. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  97. }
  98. } else {
  99. szExpandedPath = G_ALLOC (INITIAL_VB_LIST_SIZE);
  100. if (szExpandedPath != NULL) {
  101. // there's a wild card path character so expand it then enter them
  102. // clear the list buffer
  103. *(LPDWORD)szExpandedPath = 0;
  104. dwSize1 = dwSize2 = INITIAL_VB_LIST_SIZE;
  105. PdhExpandCounterPath (NewCounterName, szExpandedPath, &dwSize2);
  106. if (dwSize2 < dwSize1) {
  107. // then the returned buffer fit
  108. for (NewCounterName2 = szExpandedPath;
  109. *NewCounterName2 != 0;
  110. NewCounterName2 += (lstrlen(NewCounterName2) + 1)) {
  111. if (!PdhiAddStringToVbList (NewCounterName2)) {
  112. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  113. break; //out of loop
  114. }
  115. }
  116. } else {
  117. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  118. }
  119. G_FREE (szExpandedPath);
  120. } else {
  121. SetLastError (PDH_MEMORY_ALLOCATION_FAILURE);
  122. }
  123. }
  124. }
  125. // clear buffer
  126. memset (pDlgConfig->szReturnPathBuffer, 0,
  127. (pDlgConfig->cchReturnPathLength * sizeof(CHAR)));
  128. }
  129. pDlgConfig->CallBackStatus = pdhStatus;
  130. return;
  131. }
  132. double
  133. PdhVbGetDoubleCounterValue (
  134. IN HCOUNTER hCounter,
  135. IN LPDWORD pdwCounterStatus
  136. )
  137. /*++
  138. Routine Description:
  139. retrieves the current value of the specified counter and returns the
  140. formatted version to the caller.
  141. Arguments:
  142. IN HCOUNTER hCounter
  143. pointer to the counter to get the data for
  144. IN LPDWORD pdwCounterStatus
  145. status value of this counter. This value should be checked to
  146. insure the data is valid. If the status is not successful, then
  147. the data returned cannot be trusted and should not be used
  148. Return Value:
  149. a double precesion floating point value of the current counter value
  150. formatted and computed as required by the counter type.
  151. --*/
  152. {
  153. PDH_STATUS pdhStatus;
  154. PDH_FMT_COUNTERVALUE pdhValue;
  155. DWORD dwCounterType;
  156. double dReturn;
  157. pdhStatus = PdhGetFormattedCounterValue (
  158. hCounter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100, &dwCounterType, &pdhValue);
  159. if (pdhStatus == ERROR_SUCCESS) {
  160. // the function was successful so return the counter status
  161. // and the returned value
  162. pdhStatus = pdhValue.CStatus;
  163. dReturn = pdhValue.doubleValue;
  164. } else {
  165. // the function returned an error so return the
  166. // error in the status field & 0.0 for a value
  167. dReturn = 0.0f;
  168. }
  169. if (pdwCounterStatus != NULL) {
  170. __try {
  171. *pdwCounterStatus = pdhStatus;
  172. } __except (EXCEPTION_EXECUTE_HANDLER) {
  173. // unable to write to status variable
  174. // don't worry about it, since it's optional and there's not much
  175. // we can do here anyway.
  176. }
  177. }
  178. return dReturn;
  179. }
  180. DWORD
  181. PdhVbGetOneCounterPath (
  182. IN LPSTR szPathBuffer,
  183. IN DWORD cchBufferLength,
  184. IN DWORD dwDetailLevel,
  185. IN LPCSTR szCaption
  186. )
  187. /*++
  188. Routine Description:
  189. Retrieves one path string from the buffer of stored counter paths
  190. assembled by the most recent call to PdhVbCreateCounterPathList
  191. Arguments:
  192. LPSTR szPathBuffer
  193. string buffer to return selected counter path in
  194. DWORD cchBufferLength
  195. size of string buffer in characters
  196. DWORD dwDetailLevel
  197. detail level to filter the counters by
  198. LPCSTR szCaption
  199. string to display in the caption bar
  200. Return Value:
  201. returns the length of the path string in characters returned
  202. to the caller.
  203. --*/
  204. {
  205. PDH_BROWSE_DLG_CONFIG_A BrowseConfig;
  206. PDH_STATUS PdhStatus = ERROR_SUCCESS;
  207. DWORD dwReturn = 0;
  208. // test access to caller supplied buffer
  209. __try {
  210. CHAR cChar;
  211. if ((cchBufferLength > 0) && (szPathBuffer != NULL)) {
  212. cChar = szPathBuffer[0];
  213. szPathBuffer[0] = 0;
  214. szPathBuffer[0] = cChar;
  215. cChar = szPathBuffer[cchBufferLength - 1];
  216. szPathBuffer[cchBufferLength - 1] = 0;
  217. szPathBuffer[cchBufferLength - 1] = cChar;
  218. } else {
  219. PdhStatus = PDH_INVALID_ARGUMENT;
  220. }
  221. if (szCaption != NULL) {
  222. cChar = *((CHAR volatile *)szCaption);
  223. }
  224. } __except (EXCEPTION_EXECUTE_HANDLER) {
  225. PdhStatus = PDH_INVALID_ARGUMENT;
  226. }
  227. if (PdhStatus == ERROR_SUCCESS) {
  228. memset (&BrowseConfig, 0, sizeof(BrowseConfig));
  229. BrowseConfig.bIncludeInstanceIndex = FALSE;
  230. BrowseConfig.bSingleCounterPerAdd = TRUE;
  231. BrowseConfig.bSingleCounterPerDialog = TRUE;
  232. BrowseConfig.bLocalCountersOnly = FALSE;
  233. BrowseConfig.bWildCardInstances = FALSE;
  234. BrowseConfig.bDisableMachineSelection = FALSE;
  235. BrowseConfig.bHideDetailBox = (dwDetailLevel > 0 ? TRUE : FALSE);
  236. BrowseConfig.hWndOwner = NULL; // there should be some way to get this
  237. BrowseConfig.szReturnPathBuffer = szPathBuffer;
  238. BrowseConfig.cchReturnPathLength = cchBufferLength;
  239. BrowseConfig.pCallBack = NULL;
  240. BrowseConfig.dwCallBackArg = 0;
  241. // default is to show ALL counters
  242. BrowseConfig.dwDefaultDetailLevel = (dwDetailLevel > 0 ?
  243. dwDetailLevel : PERF_DETAIL_WIZARD);
  244. BrowseConfig.szDialogBoxCaption = (LPSTR)szCaption;
  245. PdhStatus = PdhBrowseCountersA (&BrowseConfig);
  246. }
  247. if (PdhStatus == ERROR_SUCCESS) {
  248. dwReturn = lstrlenA (szPathBuffer);
  249. } else {
  250. dwReturn = 0;
  251. }
  252. return dwReturn;
  253. }
  254. DWORD
  255. PdhVbCreateCounterPathList (
  256. IN DWORD dwDetailLevel,
  257. IN LPCSTR szCaption
  258. )
  259. /*++
  260. Routine Description:
  261. Displays the counter browsing dialog box and allows the user to select
  262. multiple counter paths. As the paths are selected, they are stored
  263. in an internal buffer for later retrieval by the caller.
  264. NOTE, that calling this function will clear any previous selections.
  265. Arguments:
  266. DWORD dwDetailLevel
  267. detail level to filter the counters by
  268. LPCSTR szCaption
  269. string to display in the caption bar
  270. Return Value:
  271. returns the number of path strings selected by the user that must
  272. be retrieved by the caller.
  273. --*/
  274. {
  275. PDH_STATUS PdhStatus = ERROR_SUCCESS;
  276. PDH_BROWSE_DLG_CONFIG_A BrowseConfig;
  277. DWORD dwReturn = 0;
  278. // test access to caller supplied buffer
  279. __try {
  280. CHAR cChar;
  281. if (szCaption != NULL) {
  282. cChar = *((CHAR volatile *)szCaption);
  283. }
  284. } __except (EXCEPTION_EXECUTE_HANDLER) {
  285. PdhStatus = PDH_INVALID_ARGUMENT;
  286. }
  287. if (PdhStatus == ERROR_SUCCESS) {
  288. if (PdhivbList.mszList != NULL) {
  289. G_FREE (PdhivbList.mszList);
  290. memset ((LPVOID)&PdhivbList, 0, sizeof (VB_STRING_LIST));
  291. }
  292. PdhivbList.mszList = G_ALLOC (INITIAL_VB_LIST_SIZE);
  293. if (PdhivbList.mszList != NULL) {
  294. PdhivbList.szLastItemRead =
  295. PdhivbList.szTermChar = PdhivbList.mszList;
  296. PdhivbList.dwRemaining =
  297. PdhivbList.dwSize = INITIAL_VB_LIST_SIZE;
  298. PdhivbList.dwLastEntryRead = 0;
  299. PdhivbList.dwLastItemLength = 0;
  300. } else {
  301. PdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  302. }
  303. }
  304. if (PdhStatus == ERROR_SUCCESS) {
  305. memset (&BrowseConfig, 0, sizeof(BrowseConfig));
  306. BrowseConfig.bIncludeInstanceIndex = FALSE;
  307. BrowseConfig.bSingleCounterPerAdd = FALSE;
  308. BrowseConfig.bSingleCounterPerDialog = FALSE;
  309. BrowseConfig.bLocalCountersOnly = FALSE;
  310. BrowseConfig.bWildCardInstances = FALSE;
  311. BrowseConfig.bDisableMachineSelection = FALSE;
  312. BrowseConfig.bHideDetailBox = (dwDetailLevel > 0 ? TRUE : FALSE);
  313. BrowseConfig.hWndOwner = NULL; // there should be some way to get this
  314. BrowseConfig.szReturnPathBuffer = G_ALLOC (INITIAL_VB_LIST_SIZE);
  315. if (BrowseConfig.szReturnPathBuffer != NULL) {
  316. BrowseConfig.cchReturnPathLength = (BrowseConfig.szReturnPathBuffer != NULL ?
  317. INITIAL_VB_LIST_SIZE : 0);
  318. BrowseConfig.pCallBack = (CounterPathCallBack)PdhiDialogCallBack;
  319. BrowseConfig.dwCallBackArg = (DWORD_PTR)&BrowseConfig;
  320. // default is to show ALL counters
  321. BrowseConfig.dwDefaultDetailLevel = (dwDetailLevel > 0 ?
  322. dwDetailLevel : PERF_DETAIL_WIZARD);
  323. BrowseConfig.szDialogBoxCaption = (LPSTR)szCaption;
  324. PdhStatus = PdhBrowseCountersA (&BrowseConfig);
  325. if (BrowseConfig.szReturnPathBuffer != NULL) {
  326. G_FREE (BrowseConfig.szReturnPathBuffer);
  327. }
  328. dwReturn = PdhivbList.dwNumEntries;
  329. } else {
  330. SetLastError (PDH_MEMORY_ALLOCATION_FAILURE);
  331. dwReturn = 0;
  332. }
  333. }
  334. return dwReturn;
  335. }
  336. DWORD
  337. PdhVbGetCounterPathFromList (
  338. IN DWORD dwIndex, // starting at 1 for VB types
  339. IN LPSTR szBuffer, // return buffer
  340. IN DWORD dwBufferSize // size in chars of buffer
  341. )
  342. /*++
  343. Routine Description:
  344. Displays the counter browsing dialog box and allows the user to select
  345. multiple counter paths. As the paths are selected, they are stored
  346. in an internal buffer for later retrieval by the caller.
  347. NOTE, that calling this function will clear any previous selections.
  348. Arguments:
  349. DWORD dwIndex
  350. The "1-based" index of the counter path to retrieve from
  351. the list of selected counter paths generated by the previous
  352. call to PdhVbCreateCounterPathList.
  353. LPSTR szBuffer
  354. string buffer to return the selected string in
  355. DWORD dwBufferSize
  356. size of the szBuffer in characters
  357. Return Value:
  358. Returns the number of characters copied to the calling function
  359. --*/
  360. {
  361. DWORD dwBuffIndex; // 0-based index for "c"
  362. DWORD dwThisIndex;
  363. DWORD dwCharsCopied; // size of string not counting term NULL
  364. BOOL bContinue = TRUE;
  365. dwBuffIndex = dwIndex - 1;
  366. dwCharsCopied = 0;
  367. // validate the arguments
  368. __try {
  369. if (dwBufferSize > 0) {
  370. // try writing to ouput buffer
  371. szBuffer[0] = 0;
  372. szBuffer[dwBufferSize-1] = 0;
  373. } else {
  374. bContinue = FALSE;
  375. }
  376. if (dwBuffIndex >= PdhivbList.dwNumEntries) {
  377. bContinue = FALSE;
  378. }
  379. } __except (EXCEPTION_EXECUTE_HANDLER) {
  380. bContinue = FALSE;
  381. }
  382. if (bContinue) {
  383. if (PdhivbList.szLastItemRead == NULL) {
  384. PdhivbList.szLastItemRead = PdhivbList.mszList;
  385. PdhivbList.dwLastEntryRead = 0;
  386. PdhivbList.dwLastItemLength = 0;
  387. }
  388. if (PdhivbList.szLastItemRead != NULL) {
  389. if (PdhivbList.dwLastItemLength == 0) {
  390. PdhivbList.dwLastItemLength =
  391. lstrlen(PdhivbList.szLastItemRead) + 1;
  392. }
  393. }
  394. else {
  395. bContinue = FALSE;
  396. }
  397. }
  398. if (bContinue) {
  399. // see if this is the next entry
  400. if (dwBuffIndex == (PdhivbList.dwLastEntryRead + 1)) {
  401. PdhivbList.szLastItemRead += PdhivbList.dwLastItemLength;
  402. PdhivbList.dwLastItemLength = lstrlen(PdhivbList.szLastItemRead) + 1;
  403. PdhivbList.dwLastEntryRead++;
  404. if (PdhivbList.dwLastItemLength < dwBufferSize) {
  405. lstrcpy (szBuffer, PdhivbList.szLastItemRead);
  406. dwCharsCopied = PdhivbList.dwLastItemLength -1;
  407. }
  408. } else if (dwBuffIndex == PdhivbList.dwLastEntryRead) {
  409. // it's this one (again)
  410. if (PdhivbList.dwLastItemLength < dwBufferSize) {
  411. lstrcpy (szBuffer, PdhivbList.szLastItemRead);
  412. dwCharsCopied = PdhivbList.dwLastItemLength -1;
  413. }
  414. } else {
  415. // walk the list to the desired entry (ugh!)
  416. PdhivbList.szLastItemRead = PdhivbList.mszList;
  417. for (dwThisIndex = 0; dwThisIndex < dwBuffIndex; dwThisIndex++) {
  418. PdhivbList.szLastItemRead += lstrlen (PdhivbList.szLastItemRead) + 1;
  419. }
  420. PdhivbList.dwLastItemLength = lstrlen(PdhivbList.szLastItemRead) + 1;
  421. PdhivbList.dwLastEntryRead = dwThisIndex;
  422. if (PdhivbList.dwLastItemLength < dwBufferSize) {
  423. lstrcpy (szBuffer, PdhivbList.szLastItemRead);
  424. dwCharsCopied = PdhivbList.dwLastItemLength -1;
  425. }
  426. }
  427. }
  428. return dwCharsCopied;
  429. }
  430. DWORD
  431. PdhVbGetCounterPathElements (
  432. IN LPCSTR szPathString,
  433. IN LPSTR szMachineName,
  434. IN LPSTR szObjectName,
  435. IN LPSTR szInstanceName,
  436. IN LPSTR szParentInstance,
  437. IN LPSTR szCounterName,
  438. IN DWORD dwBufferSize
  439. )
  440. /*++
  441. Routine Description:
  442. breaks the counter path provided in the szPathString argument and
  443. returns the components in the buffers provided by the caller.
  444. The buffers must be at least "dwBufferSize" in length.
  445. Arguments:
  446. LPCSTR szPathString
  447. pointer to the full counter path that is to be parsed into
  448. component strings
  449. LPSTR szMachineName
  450. caller supplied buffer that is to receive the machine name.
  451. The buffer must be at least dwBufferSize in length.
  452. LPSTR szObjectName
  453. caller supplied buffer that is to receive the object name.
  454. The buffer must be at least dwBufferSize in length.
  455. LPSTR szInstanceName
  456. caller supplied buffer that is to receive the Instance name.
  457. The buffer must be at least dwBufferSize in length.
  458. LPSTR szParentInstance
  459. caller supplied buffer that is to receive the parent instance name.
  460. The buffer must be at least dwBufferSize in length.
  461. LPSTR szCounterName
  462. caller supplied buffer that is to receive the counter name.
  463. The buffer must be at least dwBufferSize in length.
  464. DWORD dwBufferSize
  465. The buffer size of the caller supplied string buffers in characters
  466. Return Value:
  467. ERROR_SUCCESS if the counter string is successfully parsed, otherwise
  468. a PDH error if not.
  469. PDH_INVALID_ARGUMENT if one or more of the string buffers is not
  470. the correct size
  471. PDH_INSUFFICIENT_BUFFER if one or more of the counter path elements
  472. is too large for the return buffer length.
  473. PDH_MEMORY_ALLOCATION_FAILURE if a temporary memory buffer could not
  474. be allocated.
  475. --*/
  476. {
  477. PPDH_COUNTER_PATH_ELEMENTS_A pInfo;
  478. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  479. DWORD dwSize;
  480. // validate the return arguments
  481. __try {
  482. CHAR cChar;
  483. if (szPathString != NULL) {
  484. cChar = *((CHAR volatile *)szPathString);
  485. if (cChar == 0) {
  486. pdhStatus = PDH_INVALID_ARGUMENT;
  487. }
  488. } else {
  489. pdhStatus = PDH_INVALID_ARGUMENT;
  490. }
  491. if (pdhStatus == ERROR_SUCCESS){
  492. if (szMachineName != NULL) {
  493. szMachineName[0] = 0;
  494. szMachineName[dwBufferSize-1] = 0;
  495. } else {
  496. pdhStatus = PDH_INVALID_ARGUMENT;
  497. }
  498. }
  499. if (pdhStatus == ERROR_SUCCESS){
  500. if (szObjectName != NULL) {
  501. szObjectName[0] = 0;
  502. szObjectName[dwBufferSize-1] = 0;
  503. } else {
  504. pdhStatus = PDH_INVALID_ARGUMENT;
  505. }
  506. }
  507. if (pdhStatus == ERROR_SUCCESS){
  508. if (szInstanceName != NULL) {
  509. szInstanceName[0] = 0;
  510. szInstanceName[dwBufferSize-1] = 0;
  511. } else {
  512. pdhStatus = PDH_INVALID_ARGUMENT;
  513. }
  514. }
  515. if (pdhStatus == ERROR_SUCCESS){
  516. if (szParentInstance != NULL) {
  517. szParentInstance[0] = 0;
  518. szParentInstance[dwBufferSize-1] = 0;
  519. } else {
  520. pdhStatus = PDH_INVALID_ARGUMENT;
  521. }
  522. }
  523. if (pdhStatus == ERROR_SUCCESS){
  524. if (szCounterName != NULL) {
  525. szCounterName[0] = 0;
  526. szCounterName[dwBufferSize-1] = 0;
  527. } else {
  528. pdhStatus = PDH_INVALID_ARGUMENT;
  529. }
  530. }
  531. } __except (EXCEPTION_EXECUTE_HANDLER) {
  532. pdhStatus = PDH_INVALID_ARGUMENT;
  533. }
  534. if (pdhStatus == ERROR_SUCCESS) {
  535. // allocate temp buffer for component strings
  536. dwSize = (5 * dwBufferSize) + sizeof (PDH_COUNTER_INFO_A);
  537. pInfo = G_ALLOC (dwSize);
  538. if (pInfo != NULL) {
  539. pdhStatus = PdhParseCounterPathA (
  540. szPathString,
  541. pInfo,
  542. &dwSize,
  543. 0);
  544. if (pdhStatus == ERROR_SUCCESS) {
  545. // move from local structure to user args if the strings will fit
  546. if (pInfo->szMachineName != NULL) {
  547. if ((DWORD)lstrlenA(pInfo->szMachineName) < dwBufferSize) {
  548. lstrcpyA (szMachineName, pInfo->szMachineName);
  549. } else {
  550. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  551. }
  552. }
  553. if (pInfo->szObjectName != NULL) {
  554. if ((DWORD)lstrlenA(pInfo->szObjectName) < dwBufferSize) {
  555. lstrcpyA (szObjectName, pInfo->szObjectName);
  556. } else {
  557. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  558. }
  559. }
  560. if (pInfo->szInstanceName != NULL) {
  561. if ((DWORD)lstrlenA(pInfo->szInstanceName) < dwBufferSize) {
  562. lstrcpyA (szInstanceName, pInfo->szInstanceName);
  563. } else {
  564. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  565. }
  566. }
  567. if (pInfo->szParentInstance != NULL) {
  568. if ((DWORD)lstrlenA(pInfo->szParentInstance) < dwBufferSize) {
  569. lstrcpyA (szParentInstance, pInfo->szParentInstance);
  570. } else {
  571. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  572. }
  573. }
  574. if (pInfo->szCounterName != NULL) {
  575. if ((DWORD)lstrlenA(pInfo->szCounterName) < dwBufferSize) {
  576. lstrcpyA (szCounterName, pInfo->szCounterName);
  577. } else {
  578. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  579. }
  580. }
  581. } // else pass error to caller
  582. G_FREE (pInfo);
  583. } else {
  584. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  585. }
  586. } // else pass error to caller
  587. return pdhStatus;
  588. }
  589. DWORD
  590. PdhVbAddCounter (
  591. IN HQUERY hQuery,
  592. IN LPCSTR szFullCounterPath,
  593. IN HCOUNTER *hCounter
  594. )
  595. /*++
  596. Routine Description:
  597. Creates and initializes a counter structure and attaches it to the
  598. specified query by calling the C function.
  599. Arguments:
  600. IN HQUERY hQuery
  601. handle of the query to attach this counter to once the counter
  602. entry has been successfully created.
  603. IN LPCSTR szFullCounterPath
  604. pointer to the path string that describes the counter to add to
  605. the query referenced above. This string must specify a single
  606. counter. Wildcard path strings are not permitted.
  607. IN HCOUNTER *phCounter
  608. pointer to the buffer that will get the handle value of the
  609. successfully created counter entry.
  610. Return Value:
  611. Returns ERROR_SUCCESS if a new query was created and initialized,
  612. and a PDH_ error value if not.
  613. PDH_INVALID_ARGUMENT is returned when one or more of the arguements
  614. is invalid or incorrect.
  615. PDH_MEMORY_ALLOCATION_FAILURE is returned when a memory buffer could
  616. not be allocated.
  617. PDH_INVALID_HANDLE is returned if the query handle is not valid.
  618. PDH_CSTATUS_NO_COUNTER is returned if the specified counter was
  619. not found
  620. PDH_CSTATUS_NO_OBJECT is returned if the specified object could
  621. not be found
  622. PDH_CSTATUS_NO_MACHINE is returned if a machine entry could not
  623. be created.
  624. PDH_CSTATUS_BAD_COUNTERNAME is returned if the counter name path
  625. string could not be parsed or interpreted
  626. PDH_CSTATUS_NO_COUNTERNAME is returned if an empty counter name
  627. path string is passed in
  628. PDH_FUNCTION_NOT_FOUND is returned if the calculation function
  629. for this counter could not be determined.
  630. --*/
  631. {
  632. DWORD dwReturn = ERROR_SUCCESS;
  633. HCOUNTER hLocalCounter = NULL;
  634. if ((hCounter == NULL) || (szFullCounterPath == NULL)) {
  635. dwReturn = PDH_INVALID_ARGUMENT;
  636. } else {
  637. dwReturn = PdhAddCounterA (hQuery, szFullCounterPath, 0, &hLocalCounter);
  638. }
  639. if (dwReturn == ERROR_SUCCESS) {
  640. __try {
  641. * hCounter = hLocalCounter;
  642. } __except (EXCEPTION_EXECUTE_HANDLER) {
  643. dwReturn = PDH_INVALID_ARGUMENT;
  644. }
  645. }
  646. return dwReturn;
  647. }
  648. DWORD
  649. PdhVbOpenQuery (
  650. IN HQUERY *phQuery
  651. )
  652. /*++
  653. Routine Description:
  654. allocates a new query structure for a VB app by calling the "C"
  655. function with the rest of the arguments supplied
  656. Arguments:
  657. IN HQUERY *phQuery
  658. pointer to the buffer that will receive the query handle opened.
  659. Return Value:
  660. Returns ERROR_SUCCESS if a new query was created and initialized,
  661. and a PDH_ error value if not.
  662. PDH_INVALID_ARGUMENT is returned when one or more of the arguements
  663. is invalid or incorrect.
  664. PDH_MEMORY_ALLOCATION_FAILURE is returned when a memory buffer could
  665. not be allocated.
  666. --*/
  667. {
  668. DWORD dwReturn = ERROR_SUCCESS;
  669. HQUERY hLocalQuery = NULL;
  670. if (phQuery == NULL) {
  671. dwReturn = PDH_INVALID_ARGUMENT;
  672. } else {
  673. dwReturn = PdhOpenQuery(NULL, 0, &hLocalQuery);
  674. }
  675. if (dwReturn == ERROR_SUCCESS) {
  676. __try {
  677. * phQuery = hLocalQuery;
  678. } __except (EXCEPTION_EXECUTE_HANDLER) {
  679. dwReturn = PDH_INVALID_ARGUMENT;
  680. }
  681. }
  682. return dwReturn;
  683. }
  684. DWORD
  685. PdhVbIsGoodStatus (
  686. IN LONG lStatus
  687. )
  688. /*++
  689. Routine Description:
  690. Checks the status severity of the PDH status value
  691. passed into the function as a binary Good (TRUE)/Bad (FALSE)
  692. value.
  693. Arguments:
  694. IN LONG lStatus
  695. Status code to test
  696. Return Value:
  697. TRUE if the status code is Success or Informational severity
  698. FALSE if the status code is Error or Warning severity
  699. --*/
  700. {
  701. BOOL bReturn;
  702. if (lStatus == ERROR_SUCCESS) {
  703. bReturn = TRUE;
  704. } else if (IsSuccessSeverity(lStatus)) {
  705. bReturn = TRUE;
  706. } else if (IsInformationalSeverity(lStatus)) {
  707. bReturn = TRUE;
  708. } else {
  709. bReturn = FALSE;
  710. }
  711. return (DWORD)bReturn;
  712. }
  713. DWORD
  714. PdhVbOpenLog (
  715. IN LPCSTR szLogFileName,
  716. IN DWORD dwAccessFlags,
  717. IN LPDWORD lpdwLogType,
  718. IN HQUERY hQuery,
  719. IN DWORD dwMaxSize,
  720. IN LPCSTR szUserCaption,
  721. IN HLOG *phLog
  722. )
  723. /*++
  724. Routine Description:
  725. Arguments:
  726. IN LPCSTR szLogFileName,
  727. IN DWORD dwAccessFlags,
  728. IN LPDWORD lpdwLogType,
  729. IN HQUERY hQuery,
  730. IN DWORD dwMaxSize,
  731. IN LPCSTR szUserCaption,
  732. IN HLOG *phLog
  733. Return Value:
  734. TRUE if the status code is Success or Informational severity
  735. FALSE if the status code is Error or Warning severity
  736. --*/
  737. {
  738. return PdhOpenLogA( szLogFileName,
  739. dwAccessFlags,
  740. lpdwLogType,
  741. hQuery,
  742. dwMaxSize,
  743. szUserCaption,
  744. phLog
  745. );
  746. }
  747. DWORD
  748. PdhVbUpdateLog (
  749. IN HLOG hLog,
  750. IN LPCSTR szUserString
  751. )
  752. /*++
  753. Routine Description:
  754. Arguments:
  755. IN HLOG hLog,
  756. IN LPCWSTR szUserString
  757. Return Value:
  758. TRUE if the status code is Success or Informational severity
  759. FALSE if the status code is Error or Warning severity
  760. --*/
  761. {
  762. return PdhUpdateLogA(hLog,
  763. szUserString
  764. );
  765. }
  766. DWORD
  767. PdhVbGetLogFileSize (
  768. IN HLOG hLog,
  769. IN LONG *lSize
  770. )
  771. /*++
  772. Routine Description:
  773. Arguments:
  774. IN HLOG hLog,
  775. IN LONGLONG *llSize
  776. Return Value:
  777. TRUE if the status code is Success or Informational severity
  778. FALSE if the status code is Error or Warning severity
  779. --*/
  780. {
  781. PDH_STATUS pdhStatus;
  782. LONGLONG llTemp;
  783. pdhStatus = PdhGetLogFileSize(hLog,
  784. &llTemp);
  785. if (pdhStatus == ERROR_SUCCESS) {
  786. if (llTemp > 0x0000000080000000) {
  787. // file size is larger than a long value
  788. pdhStatus = PDH_INSUFFICIENT_BUFFER;
  789. } else {
  790. __try {
  791. *lSize = (LONG)(llTemp & 0x000000007FFFFFFF);
  792. } __except (EXCEPTION_EXECUTE_HANDLER) {
  793. pdhStatus = PDH_INVALID_ARGUMENT;
  794. }
  795. }
  796. }
  797. return pdhStatus;
  798. }