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.

394 lines
10 KiB

  1. // dphcidlg.cpp : implementation file
  2. //
  3. #include <tchar.h>
  4. #include "stdafx.h"
  5. #include "DPH_TEST.h"
  6. #include "dphcidlg.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char BASED_CODE THIS_FILE[] = __FILE__;
  10. #endif
  11. #define LODWORD(x) ((DWORD)((x) & 0x00000000FFFFFFFF))
  12. #define HIDWORD(x) ((DWORD)(((x)>>32) & 0x00000000FFFFFFFF))
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CDphCounterInfoDlg dialog
  15. CDphCounterInfoDlg::CDphCounterInfoDlg(CWnd* pParent /*=NULL*/,
  16. HCOUNTER hCounterArg /*=NULL*/,
  17. HQUERY hQueryArg /*=NULL*/)
  18. : CDialog(CDphCounterInfoDlg::IDD, pParent)
  19. {
  20. //{{AFX_DATA_INIT(CDphCounterInfoDlg)
  21. // NOTE: the ClassWizard will add member initialization here
  22. //}}AFX_DATA_INIT
  23. // save this counter's handle for updates
  24. hCounter = hCounterArg;
  25. hQuery = hQueryArg;
  26. // init the timer variables
  27. nTimerId = 0;
  28. bTimerRunning = FALSE;
  29. bGetDefinitions = TRUE;
  30. // clear the statistical data buffer
  31. memset (RawDataArray, 0, sizeof(RawDataArray));
  32. dwFirstIndex = 0;
  33. dwNextIndex = 0;
  34. dwItemsUsed = 0;
  35. }
  36. void CDphCounterInfoDlg::DoDataExchange(CDataExchange* pDX)
  37. {
  38. CDialog::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CDphCounterInfoDlg)
  40. // NOTE: the ClassWizard will add DDX and DDV calls here
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CDphCounterInfoDlg, CDialog)
  44. //{{AFX_MSG_MAP(CDphCounterInfoDlg)
  45. ON_BN_CLICKED(IDC_GET_NEW_DATA, OnGetNewData)
  46. ON_BN_CLICKED(IDC_1SEC_BTN, On1secBtn)
  47. ON_WM_TIMER()
  48. ON_WM_DESTROY()
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CDphCounterInfoDlg message handlers
  53. BOOL CDphCounterInfoDlg::OnInitDialog()
  54. {
  55. CDialog::OnInitDialog();
  56. OnGetNewData();
  57. SetDefID (IDC_GET_NEW_DATA);
  58. // set button text to reflect current state
  59. SetDlgItemText (IDC_1SEC_BTN, TEXT("&Start Timer"));
  60. // clear statistics display
  61. SetDlgItemText (IDC_CI_DATA_POINTS, TEXT(""));
  62. SetDlgItemText (IDC_CI_AVERAGE_VALUE, TEXT(""));
  63. SetDlgItemText (IDC_CI_MIN_VALUE, TEXT(""));
  64. SetDlgItemText (IDC_CI_MAX_VALUE, TEXT(""));
  65. return FALSE; // return TRUE unless you set the focus to a control
  66. // EXCEPTION: OCX Property Pages should return FALSE
  67. }
  68. void CDphCounterInfoDlg::OnGetNewData()
  69. {
  70. PPDH_COUNTER_INFO pciData;
  71. PDH_RAW_COUNTER RawData;
  72. PDH_STATUS Status;
  73. DWORD dwCounterType;
  74. DWORD dwBufLen = 4096;
  75. LPTSTR szTextBuffer;
  76. PDH_FMT_COUNTERVALUE dfValue;
  77. double dValue;
  78. LONGLONG llValue;
  79. LONG lValue;
  80. SYSTEMTIME stTimeStamp;
  81. pciData = (PPDH_COUNTER_INFO)new char[dwBufLen];
  82. szTextBuffer = new TCHAR[dwBufLen];
  83. if ((pciData == NULL) || (szTextBuffer == NULL)) {
  84. Status = ERROR_OUTOFMEMORY;
  85. } else {
  86. Status = ERROR_SUCCESS;
  87. }
  88. if (Status == ERROR_SUCCESS) {
  89. Status = PdhCollectQueryData (hQuery);
  90. if (Status == PDH_NO_MORE_DATA) {
  91. if (bTimerRunning) {
  92. // stop the timer to prevent updating the display
  93. KillTimer (nTimerId);
  94. nTimerId = 0;
  95. bTimerRunning = FALSE;
  96. SetDlgItemText (IDC_1SEC_BTN, TEXT("&Start Timer"));
  97. }
  98. // disable the data buttons
  99. GetDlgItem(IDC_1SEC_BTN)->EnableWindow(FALSE);
  100. GetDlgItem(IDC_GET_NEW_DATA)->EnableWindow(FALSE);
  101. } else {
  102. // update the display values
  103. Status = PdhGetRawCounterValue (
  104. hCounter,
  105. &dwCounterType,
  106. &RawData);
  107. if ((Status == ERROR_SUCCESS) && bGetDefinitions) {
  108. Status = PdhGetCounterInfo (
  109. hCounter,
  110. TRUE,
  111. &dwBufLen,
  112. pciData);
  113. }
  114. if (Status == ERROR_SUCCESS) {
  115. // get formatted values
  116. Status = PdhGetFormattedCounterValue (
  117. hCounter,
  118. PDH_FMT_LONG,
  119. NULL,
  120. &dfValue);
  121. if (Status == ERROR_SUCCESS) {
  122. lValue = dfValue.longValue;
  123. } else {
  124. lValue = (LONG)-1;
  125. }
  126. }
  127. if (Status == ERROR_SUCCESS) {
  128. // get formatted values
  129. Status = PdhGetFormattedCounterValue (
  130. hCounter,
  131. PDH_FMT_DOUBLE,
  132. NULL,
  133. &dfValue);
  134. if (Status == ERROR_SUCCESS) {
  135. dValue = dfValue.doubleValue;
  136. } else {
  137. dValue = (double)-1.0;
  138. }
  139. }
  140. if (Status == ERROR_SUCCESS) {
  141. // get formatted values
  142. Status = PdhGetFormattedCounterValue (
  143. hCounter,
  144. PDH_FMT_LARGE,
  145. NULL,
  146. &dfValue);
  147. if (Status == ERROR_SUCCESS) {
  148. llValue = dfValue.largeValue;
  149. } else {
  150. llValue = (LONGLONG)-1;
  151. }
  152. }
  153. }
  154. }
  155. if (Status == ERROR_SUCCESS) {
  156. if (bGetDefinitions) {
  157. bGetDefinitions = FALSE; // only do once
  158. // load dialog box
  159. _stprintf (szTextBuffer, TEXT("%d"), pciData->dwLength);
  160. SetDlgItemText (IDC_CI_LENGTH, szTextBuffer);
  161. _stprintf (szTextBuffer, TEXT("0x%8.8x"), pciData->dwType);
  162. SetDlgItemText (IDC_CI_TYPE, szTextBuffer);
  163. _stprintf (szTextBuffer, TEXT("0x%8.8x"), pciData->CVersion);
  164. SetDlgItemText (IDC_CI_VERSION, szTextBuffer);
  165. _stprintf (szTextBuffer, TEXT("0x%8.8x"), pciData->CStatus);
  166. SetDlgItemText (IDC_CI_STATUS, szTextBuffer);
  167. _stprintf (szTextBuffer, TEXT("%d"), pciData->lScale);
  168. SetDlgItemText (IDC_CI_SCALE, szTextBuffer);
  169. _stprintf (szTextBuffer, TEXT("%d"), pciData->lDefaultScale);
  170. SetDlgItemText (IDC_CI_DEFAULT_SCALE, szTextBuffer);
  171. _stprintf (szTextBuffer, TEXT("0x%8.8x"), pciData->dwUserData);
  172. SetDlgItemText (IDC_CI_USER_DATA, szTextBuffer);
  173. SetDlgItemText (IDC_CI_MACHINE_NAME, pciData->szMachineName);
  174. SetDlgItemText (IDC_CI_OBJECT_NAME, pciData->szObjectName);
  175. _stprintf (szTextBuffer, TEXT("%s:%d"),
  176. pciData->szInstanceName,
  177. pciData->dwInstanceIndex);
  178. SetDlgItemText (IDC_CI_INSTANCE_NAME, szTextBuffer);
  179. SetDlgItemText (IDC_CI_PARENT_NAME, pciData->szParentInstance);
  180. SetDlgItemText (IDC_CI_COUNTER_NAME, pciData->szCounterName);
  181. SetDlgItemText (IDC_CI_EXPLAIN_TEXT, pciData->szExplainText);
  182. SetDlgItemText (IDC_CI_FULL_NAME, pciData->szFullPath);
  183. }
  184. _stprintf (szTextBuffer, TEXT("0x%8.8x"), RawData.CStatus);
  185. SetDlgItemText (IDC_RV_STATUS, szTextBuffer);
  186. FileTimeToSystemTime ((LPFILETIME)&RawData.TimeStamp, &stTimeStamp);
  187. _stprintf (szTextBuffer, TEXT("%2.2d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%3.3d"),
  188. stTimeStamp.wMonth,
  189. stTimeStamp.wDay,
  190. stTimeStamp.wYear,
  191. stTimeStamp.wHour,
  192. stTimeStamp.wMinute,
  193. stTimeStamp.wSecond,
  194. stTimeStamp.wMilliseconds);
  195. SetDlgItemText (IDC_TIME_STAMP, szTextBuffer);
  196. _stprintf (szTextBuffer, TEXT("0x%8.8x%8.8x"),
  197. HIDWORD(RawData.FirstValue),
  198. LODWORD(RawData.FirstValue));
  199. SetDlgItemText (IDC_RV_FIRST, szTextBuffer);
  200. _stprintf (szTextBuffer, TEXT("0x%8.8x%8.8x"),
  201. HIDWORD(RawData.SecondValue),
  202. LODWORD(RawData.SecondValue));
  203. SetDlgItemText (IDC_RV_SECOND, szTextBuffer);
  204. _stprintf (szTextBuffer, TEXT("0x%8.8x"), dwCounterType);
  205. SetDlgItemText (IDC_RV_TYPE, szTextBuffer);
  206. _stprintf (szTextBuffer, TEXT("%d"), lValue);
  207. SetDlgItemText (IDC_FMT_LONG_VALUE, szTextBuffer);
  208. _stprintf (szTextBuffer, TEXT("%f"), dValue);
  209. SetDlgItemText (IDC_FMT_DBL_VALUE, szTextBuffer);
  210. _stprintf (szTextBuffer, TEXT("0x%8.8x%8.8x"),
  211. HIDWORD(llValue), LODWORD(llValue));
  212. SetDlgItemText (IDC_FMT_LARGE_VALUE, szTextBuffer);
  213. } else if (Status != PDH_NO_MORE_DATA) {
  214. // clear fields and display error message
  215. // in explain text
  216. // if any error but the end of the log file
  217. szTextBuffer[0] = 0;
  218. SetDlgItemText (IDC_CI_LENGTH, szTextBuffer);
  219. SetDlgItemText (IDC_CI_TYPE, szTextBuffer);
  220. SetDlgItemText (IDC_CI_VERSION, szTextBuffer);
  221. SetDlgItemText (IDC_CI_STATUS, szTextBuffer);
  222. SetDlgItemText (IDC_CI_SCALE, szTextBuffer);
  223. SetDlgItemText (IDC_CI_USER_DATA, szTextBuffer);
  224. SetDlgItemText (IDC_CI_FULL_NAME, szTextBuffer);
  225. SetDlgItemText (IDC_CI_COUNTER_NAME, szTextBuffer);
  226. SetDlgItemText (IDC_RV_STATUS, szTextBuffer);
  227. SetDlgItemText (IDC_RV_FIRST, szTextBuffer);
  228. SetDlgItemText (IDC_RV_SECOND, szTextBuffer);
  229. SetDlgItemText (IDC_RV_TYPE, szTextBuffer);
  230. SetDlgItemText (IDC_FMT_LONG_VALUE, szTextBuffer);
  231. SetDlgItemText (IDC_FMT_DBL_VALUE, szTextBuffer);
  232. SetDlgItemText (IDC_FMT_LARGE_VALUE, szTextBuffer);
  233. _stprintf (szTextBuffer, TEXT("Unable to query counter data, Error 0x%8.8x"),
  234. Status);
  235. SetDlgItemText (IDC_CI_EXPLAIN_TEXT, szTextBuffer);
  236. } else {
  237. _stprintf (szTextBuffer, TEXT("End of Log File Reached."),
  238. Status);
  239. SetDlgItemText (IDC_CI_EXPLAIN_TEXT, szTextBuffer);
  240. }
  241. if (pciData != NULL) delete pciData;
  242. if (szTextBuffer != NULL) delete szTextBuffer;
  243. }
  244. void CDphCounterInfoDlg::On1secBtn()
  245. {
  246. if (bTimerRunning) {
  247. KillTimer (nTimerId);
  248. nTimerId = 0;
  249. bTimerRunning = FALSE;
  250. SetDlgItemText (IDC_1SEC_BTN, TEXT("&Start Timer"));
  251. // clear display
  252. SetDlgItemText (IDC_CI_DATA_POINTS, TEXT(""));
  253. SetDlgItemText (IDC_CI_AVERAGE_VALUE, TEXT(""));
  254. SetDlgItemText (IDC_CI_MIN_VALUE, TEXT(""));
  255. SetDlgItemText (IDC_CI_MAX_VALUE, TEXT(""));
  256. } else {
  257. nTimerId = SetTimer (ID_TIMER, 1000, NULL);
  258. if (nTimerId != 0) {
  259. bTimerRunning = TRUE;
  260. SetDlgItemText (IDC_1SEC_BTN, TEXT("&Stop Timer"));
  261. } else {
  262. MessageBeep (0xFFFFFFFF); //standard beep
  263. }
  264. }
  265. }
  266. void CDphCounterInfoDlg::OnTimer(UINT nIDEvent)
  267. {
  268. PDH_RAW_COUNTER RawDataItem;
  269. PDH_STATUS Status;
  270. DWORD dwCounterType;
  271. PDH_STATISTICS CtrStats;
  272. TCHAR szValue[MAX_PATH];
  273. // get data
  274. OnGetNewData();
  275. Status = PdhGetRawCounterValue (
  276. hCounter,
  277. &dwCounterType,
  278. &RawDataItem);
  279. // add raw value to array
  280. RawDataArray[dwNextIndex] = RawDataItem;
  281. // get statistics on raw data
  282. Status = PdhComputeCounterStatistics (
  283. hCounter,
  284. PDH_FMT_DOUBLE,
  285. dwFirstIndex,
  286. ++dwItemsUsed,
  287. RawDataArray,
  288. &CtrStats);
  289. // display in window
  290. if (Status == ERROR_SUCCESS) {
  291. _stprintf (szValue, TEXT("%d/%d"), CtrStats.count, dwItemsUsed);
  292. SetDlgItemText (IDC_CI_DATA_POINTS, szValue);
  293. _stprintf (szValue, TEXT("%f"), CtrStats.mean.doubleValue);
  294. SetDlgItemText (IDC_CI_AVERAGE_VALUE, szValue);
  295. _stprintf (szValue, TEXT("%f"), CtrStats.min.doubleValue);
  296. SetDlgItemText (IDC_CI_MIN_VALUE, szValue);
  297. _stprintf (szValue, TEXT("%f"), CtrStats.max.doubleValue);
  298. SetDlgItemText (IDC_CI_MAX_VALUE, szValue);
  299. } else {
  300. _stprintf (szValue, TEXT("%d"), dwItemsUsed);
  301. SetDlgItemText (IDC_CI_DATA_POINTS, szValue);
  302. // unable to compute statistics
  303. lstrcpy (szValue, TEXT("Error"));
  304. SetDlgItemText (IDC_CI_AVERAGE_VALUE, szValue);
  305. SetDlgItemText (IDC_CI_MIN_VALUE, szValue);
  306. SetDlgItemText (IDC_CI_MAX_VALUE, szValue);
  307. }
  308. // update pointers & indeces
  309. if (dwItemsUsed < RAW_DATA_ITEMS) {
  310. dwNextIndex = ++dwNextIndex % RAW_DATA_ITEMS;
  311. } else {
  312. --dwItemsUsed;
  313. dwNextIndex = dwFirstIndex;
  314. dwFirstIndex = ++dwFirstIndex % RAW_DATA_ITEMS;
  315. }
  316. CDialog::OnTimer(nIDEvent);
  317. }
  318. void CDphCounterInfoDlg::OnDestroy()
  319. {
  320. CDialog::OnDestroy();
  321. if (bTimerRunning) {
  322. KillTimer (nTimerId);
  323. nTimerId = 0;
  324. bTimerRunning = FALSE;
  325. }
  326. }
  327.