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.

207 lines
5.4 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. browse.cpp
  5. Abstract:
  6. Implements the interaction with the PDH browser dialog.
  7. --*/
  8. #include <assert.h>
  9. #include "polyline.h"
  10. #include "pdhmsg.h"
  11. #include "browser.h"
  12. #include "smonmsg.h"
  13. #include "utils.h"
  14. typedef struct {
  15. PDH_BROWSE_DLG_CONFIG_H *pBrowseInfo;
  16. ENUMPATH_CALLBACK pCallback;
  17. LPVOID lpUserData;
  18. } ENUMCALLBACK_INFO;
  19. static PDH_FUNCTION
  20. BrowseCallback (
  21. DWORD_PTR lpParam
  22. );
  23. HRESULT
  24. BrowseCounters (
  25. HLOG hDataSource,
  26. DWORD dwDetailLevel,
  27. HWND hwndOwner,
  28. ENUMPATH_CALLBACK pCallback,
  29. LPVOID lpUserData,
  30. BOOL bUseInstanceIndex
  31. )
  32. {
  33. #define CTRBUFLEN 8192
  34. PDH_BROWSE_DLG_CONFIG_H BrowseInfo;
  35. ENUMCALLBACK_INFO CallbackInfo;
  36. // clear the structure before assigning values
  37. memset (&BrowseInfo, 0, sizeof (BrowseInfo));
  38. BrowseInfo.bIncludeInstanceIndex = (bUseInstanceIndex ? 1 : 0);
  39. BrowseInfo.bSingleCounterPerAdd = 0;
  40. BrowseInfo.bSingleCounterPerDialog = 0;
  41. BrowseInfo.bLocalCountersOnly = 0;
  42. BrowseInfo.bWildCardInstances = 1;
  43. BrowseInfo.bHideDetailBox = 1;
  44. BrowseInfo.bInitializePath = 0;
  45. BrowseInfo.bDisableMachineSelection = 0;
  46. BrowseInfo.bReserved = 0;
  47. BrowseInfo.bIncludeCostlyObjects = 0;
  48. BrowseInfo.szDialogBoxCaption = ResourceString(IDS_ADDCOUNTERS);
  49. BrowseInfo.hWndOwner = hwndOwner;
  50. BrowseInfo.hDataSource = hDataSource;
  51. BrowseInfo.dwDefaultDetailLevel = dwDetailLevel;
  52. BrowseInfo.szReturnPathBuffer = (LPTSTR)malloc(CTRBUFLEN * sizeof(TCHAR));
  53. if (BrowseInfo.szReturnPathBuffer == NULL)
  54. return E_OUTOFMEMORY;
  55. BrowseInfo.cchReturnPathLength = CTRBUFLEN;
  56. CallbackInfo.pBrowseInfo = &BrowseInfo;
  57. CallbackInfo.pCallback = pCallback;
  58. CallbackInfo.lpUserData = lpUserData;
  59. BrowseInfo.dwCallBackArg = (DWORD_PTR)&CallbackInfo;
  60. BrowseInfo.pCallBack = BrowseCallback;
  61. //assert( IsWindowUnicode( hWndOwner ) );
  62. PdhBrowseCountersH (&BrowseInfo);
  63. if (BrowseInfo.szReturnPathBuffer)
  64. free(BrowseInfo.szReturnPathBuffer);
  65. return NO_ERROR;
  66. }
  67. static PDH_FUNCTION
  68. BrowseCallback (
  69. DWORD_PTR dwParam
  70. )
  71. {
  72. #define CTRBUFLIMIT (0x7fffffff)
  73. HRESULT hr = S_OK;
  74. BOOLEAN fDuplicate = FALSE;
  75. ENUMCALLBACK_INFO *pCallbackInfo = (ENUMCALLBACK_INFO*)dwParam;
  76. PDH_BROWSE_DLG_CONFIG_H *pBrowseInfo = pCallbackInfo->pBrowseInfo;
  77. LPTSTR pszCtrPath;
  78. if (pBrowseInfo->CallBackStatus == ERROR_SUCCESS) {
  79. // Call callback for each path
  80. // If wildcard path, EnumExpandedPath will call once for each generated path
  81. for (pszCtrPath = pBrowseInfo->szReturnPathBuffer;
  82. *pszCtrPath != 0;
  83. pszCtrPath += (lstrlen(pszCtrPath) + 1)) {
  84. hr = EnumExpandedPath(pBrowseInfo->hDataSource, pszCtrPath,
  85. pCallbackInfo->pCallback, pCallbackInfo->lpUserData);
  86. if (hr == SMON_STATUS_DUPL_COUNTER_PATH)
  87. fDuplicate = TRUE;
  88. }
  89. // Notify user if duplicates encountered
  90. if (fDuplicate)
  91. MessageBox(pBrowseInfo->hWndOwner, ResourceString(IDS_DUPL_PATH_ERR), ResourceString(IDS_APP_NAME),
  92. MB_OK | MB_ICONWARNING);
  93. } else if (pBrowseInfo->CallBackStatus == PDH_MORE_DATA
  94. && pBrowseInfo->cchReturnPathLength < CTRBUFLIMIT) {
  95. // Malloc no longer limited to 64K
  96. free(pBrowseInfo->szReturnPathBuffer);
  97. pBrowseInfo->cchReturnPathLength *= 2;
  98. pBrowseInfo->szReturnPathBuffer = (TCHAR*)malloc(pBrowseInfo->cchReturnPathLength * sizeof(TCHAR));
  99. if (pBrowseInfo->szReturnPathBuffer)
  100. return PDH_RETRY;
  101. }
  102. return ERROR_SUCCESS;
  103. }
  104. HRESULT
  105. EnumExpandedPath (
  106. HLOG hDataSource,
  107. LPTSTR pszCtrPath,
  108. ENUMPATH_CALLBACK pCallback,
  109. LPVOID lpUserData
  110. )
  111. {
  112. #define INSTBUFLEN 4096
  113. PDH_STATUS stat = ERROR_SUCCESS;
  114. ULONG ulBufLen;
  115. INT nInstBufRetry;
  116. LPTSTR pszInstBuf = NULL;
  117. LPTSTR pszInstance;
  118. // If no wild card, invoke callback once on path
  119. if (_tcschr(pszCtrPath, TEXT('*')) == NULL) {
  120. return pCallback(pszCtrPath, (DWORD_PTR)lpUserData, 0);
  121. }
  122. ulBufLen = INSTBUFLEN;
  123. nInstBufRetry = 10; // the retry counter
  124. do {
  125. if ( NULL != pszInstBuf ) {
  126. free(pszInstBuf);
  127. pszInstBuf = NULL;
  128. ulBufLen *= 2;
  129. }
  130. pszInstBuf = (TCHAR*) malloc(ulBufLen * sizeof(TCHAR));
  131. if (pszInstBuf == NULL) {
  132. stat = E_OUTOFMEMORY;
  133. break;
  134. }
  135. stat = PdhExpandWildCardPathH (
  136. hDataSource,
  137. pszCtrPath,
  138. pszInstBuf,
  139. &ulBufLen,
  140. 0);
  141. nInstBufRetry--;
  142. } while ((stat == PDH_MORE_DATA) && (nInstBufRetry));
  143. if (stat == ERROR_SUCCESS) {
  144. // For each instance name, generate a path and invoke the callback
  145. for (pszInstance = pszInstBuf;
  146. *pszInstance != 0;
  147. pszInstance += lstrlen(pszInstance) + 1) {
  148. // Invoke callback
  149. HRESULT hr = pCallback(pszInstance, (DWORD_PTR)lpUserData, BROWSE_WILDCARD);
  150. // When expanding a wildcard, don't notify user about duplicate path errors
  151. if (hr != S_OK && hr != SMON_STATUS_DUPL_COUNTER_PATH)
  152. stat = hr;
  153. }
  154. }
  155. if (pszInstBuf)
  156. free(pszInstBuf);
  157. return stat;
  158. }