Leaked source code of windows server 2003
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.

911 lines
30 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. maindlg.c
  5. Abstract:
  6. Main Dialog procedure for ShowPerf app
  7. Author:
  8. Bob Watson (a-robw)
  9. Revision History:
  10. 23 Nov 94
  11. --*/
  12. #include <windows.h>
  13. #include <winperf.h>
  14. #include <strsafe.h>
  15. #include "showperf.h"
  16. #include "perfdata.h"
  17. #include "resource.h"
  18. PPERF_DATA_BLOCK pMainPerfData = NULL; // pointer to perfdata block
  19. LPWSTR * szNameTable = NULL; // pointer to perf name table
  20. DWORD dwLastName = 0;
  21. WCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 3];
  22. WCHAR szThisComputerName[MAX_COMPUTERNAME_LENGTH + 3];
  23. HKEY hKeyMachine = NULL;
  24. HKEY hKeyPerformance = NULL;
  25. #define NUM_TAB_STOPS 3
  26. INT nDataListTabs[NUM_TAB_STOPS] = { 26, 160, 235 };
  27. LPCWSTR cszEmptyString = L"";
  28. BOOL
  29. LoadObjectList(
  30. HWND hDlg,
  31. LPCWSTR szMatchItem
  32. )
  33. {
  34. PPERF_OBJECT_TYPE pObject;
  35. HWND hWndObjectCB;
  36. UINT nInitial = 0;
  37. UINT nIndex;
  38. WCHAR szNameBuffer[MAX_PATH];
  39. DWORD dwThisObject = 0;
  40. DWORD dwCounterType;
  41. BOOL bReturn = TRUE;
  42. HRESULT hError;
  43. hWndObjectCB = GetDlgItem(hDlg, IDC_OBJECT);
  44. dwCounterType = (IsDlgButtonChecked(hDlg, IDC_INCLUDE_COSTLY) == CHECKED) ? (1) : (0);
  45. // get current data block
  46. if (GetSystemPerfData(hKeyPerformance, & pMainPerfData, dwCounterType) == ERROR_SUCCESS) {
  47. // data acquired so clear combo and display
  48. SendMessageW(hWndObjectCB, CB_RESETCONTENT, 0, 0);
  49. __try {
  50. pObject = FirstObject(pMainPerfData);
  51. for (dwThisObject = 0; dwThisObject < pMainPerfData->NumObjectTypes; dwThisObject ++) {
  52. // get counter object name here...
  53. hError = StringCchPrintfW(szNameBuffer, RTL_NUMBER_OF(szNameBuffer), L"(%d) %ws",
  54. pObject->ObjectNameTitleIndex,
  55. pObject->ObjectNameTitleIndex <= dwLastName ?
  56. szNameTable[pObject->ObjectNameTitleIndex] : L"Name not loaded");
  57. if (SUCCEEDED(hError)) {
  58. nIndex = (UINT) SendMessageW(hWndObjectCB, CB_INSERTSTRING, (WPARAM) -1, (LPARAM) szNameBuffer);
  59. if (nIndex != CB_ERR) {
  60. // save object pointer
  61. SendMessageW(hWndObjectCB, CB_SETITEMDATA, (WPARAM) nIndex, (LPARAM) pObject);
  62. if (pObject->ObjectNameTitleIndex == (DWORD) pMainPerfData->DefaultObject) {
  63. // remember this index to set the default object
  64. nInitial = nIndex;
  65. }
  66. }
  67. }
  68. pObject = NextObject(pObject);
  69. }
  70. }
  71. __except (EXCEPTION_EXECUTE_HANDLER) {
  72. hError = StringCchPrintfW(szNameBuffer, RTL_NUMBER_OF(szNameBuffer),
  73. L"An exception (0x%8.8x) occured in object block # %d returned by the system.",
  74. GetExceptionCode (), dwThisObject + 1);
  75. if (SUCCEEDED(hError)) {
  76. MessageBoxW(hDlg, szNameBuffer, L"Data Error", MB_OK);
  77. }
  78. // update the data buffer so that only the valid objects
  79. // are accessed in the future.
  80. pMainPerfData->NumObjectTypes = dwThisObject - 1;
  81. }
  82. if (szMatchItem == NULL) {
  83. SendMessageW(hWndObjectCB, CB_SETCURSEL, (WPARAM) nInitial, 0);
  84. }
  85. else {
  86. // match to arg string as best as possible
  87. if (SendMessageW(hWndObjectCB, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) szMatchItem) == CB_ERR) {
  88. // no match found so use default
  89. SendMessageW(hWndObjectCB, CB_SETCURSEL, (WPARAM) nInitial, 0);
  90. }
  91. }
  92. }
  93. else {
  94. DisplayMessageBox(hDlg, IDS_UNABLE_GET_DATA, IDS_APP_ERROR, MB_OK);
  95. bReturn = FALSE;
  96. }
  97. return bReturn;
  98. }
  99. BOOL
  100. LoadInstanceList(
  101. HWND hDlg,
  102. LPCWSTR szMatchItem
  103. )
  104. {
  105. PPERF_OBJECT_TYPE pObject;
  106. PPERF_OBJECT_TYPE pParentObject;
  107. PPERF_COUNTER_BLOCK pCounterBlock;
  108. PPERF_INSTANCE_DEFINITION pInstance;
  109. PPERF_INSTANCE_DEFINITION pParentInstance;
  110. UINT nCbSel;
  111. LONG lThisInstance;
  112. WCHAR szNameBuffer[SMALL_BUFFER_SIZE];
  113. WCHAR szParentName[SMALL_BUFFER_SIZE];
  114. UINT nIndex;
  115. HRESULT hError;
  116. nCbSel = (UINT) SendDlgItemMessageW(hDlg, IDC_OBJECT, CB_GETCURSEL, 0, 0);
  117. if (nCbSel != CB_ERR) {
  118. pObject = (PPERF_OBJECT_TYPE) SendDlgItemMessageW(hDlg, IDC_OBJECT, CB_GETITEMDATA, (WPARAM) nCbSel, 0);
  119. if (pObject->NumInstances == PERF_NO_INSTANCES) {
  120. // no instances so...
  121. // clear old contents
  122. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_RESETCONTENT, 0, 0);
  123. // add display text
  124. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_INSERTSTRING, (WPARAM)-1, (LPARAM) L"<No Instances>");
  125. // select this (and only) string
  126. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETCURSEL, 0, 0);
  127. // get pointer to counter data
  128. pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pObject + pObject->DefinitionLength);
  129. // and save it as item data
  130. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETITEMDATA, 0, (LPARAM) pCounterBlock);
  131. // finally grey the window to prevent selections
  132. EnableWindow(GetDlgItem(hDlg, IDC_INSTANCE), FALSE);
  133. }
  134. else {
  135. //enable window
  136. EnableWindow(GetDlgItem(hDlg, IDC_INSTANCE), TRUE);
  137. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_RESETCONTENT, 0, 0);
  138. pInstance = FirstInstance(pObject);
  139. for (lThisInstance = 0; lThisInstance < pObject->NumInstances; lThisInstance ++) {
  140. pParentObject = GetObjectDefByTitleIndex(pMainPerfData, pInstance->ParentObjectTitleIndex);
  141. if (pParentObject != NULL) {
  142. pParentInstance = GetInstance(pParentObject, pInstance->ParentObjectInstance);
  143. }
  144. else {
  145. pParentInstance = NULL;
  146. }
  147. if (pParentInstance != NULL) {
  148. if (pParentInstance->UniqueID < 0) {
  149. // use the instance name
  150. hError = StringCchPrintfW(szParentName, RTL_NUMBER_OF(szParentName), L"%ws==>",
  151. (LPWSTR) ((LPBYTE) pParentInstance+pParentInstance->NameOffset));
  152. }
  153. else {
  154. // use the instance number
  155. hError = StringCchPrintfW(szParentName, RTL_NUMBER_OF(szParentName), L"[%d]==>",
  156. pParentInstance->UniqueID);
  157. }
  158. }
  159. else {
  160. // unknown parent
  161. * szParentName = L'\0';
  162. }
  163. ZeroMemory(szNameBuffer, sizeof(szNameBuffer));
  164. if (pInstance->UniqueID < 0) {
  165. // use the instance name
  166. hError = StringCchCopyW(szNameBuffer,
  167. RTL_NUMBER_OF(szNameBuffer),
  168. (LPWSTR) ((LPBYTE)pInstance+pInstance->NameOffset));
  169. }
  170. else {
  171. // use the instance number
  172. hError = StringCchPrintfW(szNameBuffer, RTL_NUMBER_OF(szNameBuffer), L"(%d)", pInstance->UniqueID);
  173. }
  174. hError = StringCchCatW(szParentName, RTL_NUMBER_OF(szNameBuffer), szNameBuffer);
  175. nIndex = (UINT) SendDlgItemMessageW(
  176. hDlg, IDC_INSTANCE, CB_INSERTSTRING, (WPARAM) -1, (LPARAM) szParentName);
  177. if (nIndex != CB_ERR) {
  178. // save pointer to counter block
  179. pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pInstance + pInstance->ByteLength);
  180. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETITEMDATA, (WPARAM) nIndex, (LPARAM) pCounterBlock);
  181. }
  182. pInstance = NextInstance(pInstance);
  183. }
  184. if (szMatchItem == NULL) {
  185. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETCURSEL, 0, 0);
  186. }
  187. else {
  188. if (SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) szMatchItem)
  189. == CB_ERR) {
  190. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETCURSEL, 0, 0);
  191. }
  192. }
  193. }
  194. }
  195. else {
  196. // no object selected
  197. // clear old contents
  198. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_RESETCONTENT, 0, 0);
  199. // add display text
  200. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_INSERTSTRING, (WPARAM) -1, (LPARAM) L"<No object selected>");
  201. // select this (and only) string
  202. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETCURSEL, 0, 0);
  203. // and save null pointer as item data
  204. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_SETITEMDATA, 0, (LPARAM) 0);
  205. // finally grey the window to prevent selections
  206. EnableWindow(GetDlgItem(hDlg, IDC_INSTANCE), FALSE);
  207. }
  208. return TRUE;
  209. }
  210. LPCWSTR
  211. GetCounterTypeName(
  212. DWORD dwCounterType
  213. )
  214. {
  215. UINT nTypeString = 0;
  216. LPCWSTR szReturn = NULL;
  217. switch (dwCounterType) {
  218. case PERF_COUNTER_COUNTER:
  219. nTypeString = IDS_TYPE_COUNTER_COUNTER;
  220. break;
  221. case PERF_COUNTER_TIMER:
  222. nTypeString = IDS_TYPE_COUNTER_TIMER;
  223. break;
  224. case PERF_COUNTER_QUEUELEN_TYPE:
  225. nTypeString = IDS_TYPE_COUNTER_QUEUELEN;
  226. break;
  227. case PERF_COUNTER_LARGE_QUEUELEN_TYPE:
  228. nTypeString = IDS_TYPE_COUNTER_LARGE_QUEUELEN;
  229. break;
  230. case PERF_COUNTER_100NS_QUEUELEN_TYPE:
  231. nTypeString = IDS_TYPE_COUNTER_100NS_QUEUELEN;
  232. break;
  233. case PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
  234. nTypeString = IDS_TYPE_COUNTER_OBJ_TIME_QUEUELEN;
  235. break;
  236. case PERF_COUNTER_BULK_COUNT:
  237. nTypeString = IDS_TYPE_COUNTER_BULK_COUNT;
  238. break;
  239. case PERF_COUNTER_TEXT:
  240. nTypeString = IDS_TYPE_COUNTER_TEXT;
  241. break;
  242. case PERF_COUNTER_RAWCOUNT:
  243. nTypeString = IDS_TYPE_COUNTER_RAWCOUNT;
  244. break;
  245. case PERF_COUNTER_LARGE_RAWCOUNT:
  246. nTypeString = IDS_TYPE_COUNTER_LARGE_RAW;
  247. break;
  248. case PERF_COUNTER_RAWCOUNT_HEX:
  249. nTypeString = IDS_TYPE_COUNTER_RAW_HEX;
  250. break;
  251. case PERF_COUNTER_LARGE_RAWCOUNT_HEX:
  252. nTypeString = IDS_TYPE_COUNTER_LARGE_RAW_HEX;
  253. break;
  254. case PERF_SAMPLE_FRACTION:
  255. nTypeString = IDS_TYPE_SAMPLE_FRACTION;
  256. break;
  257. case PERF_SAMPLE_COUNTER:
  258. nTypeString = IDS_TYPE_SAMPLE_COUNTER;
  259. break;
  260. case PERF_COUNTER_NODATA:
  261. nTypeString = IDS_TYPE_COUNTER_NODATA;
  262. break;
  263. case PERF_COUNTER_TIMER_INV:
  264. nTypeString = IDS_TYPE_COUNTER_TIMER_INV;
  265. break;
  266. case PERF_SAMPLE_BASE:
  267. nTypeString = IDS_TYPE_SAMPLE_BASE;
  268. break;
  269. case PERF_AVERAGE_TIMER:
  270. nTypeString = IDS_TYPE_AVERAGE_TIMER;
  271. break;
  272. case PERF_AVERAGE_BASE:
  273. nTypeString = IDS_TYPE_AVERAGE_BASE;
  274. break;
  275. case PERF_AVERAGE_BULK:
  276. nTypeString = IDS_TYPE_AVERAGE_BULK;
  277. break;
  278. case PERF_OBJ_TIME_TIMER:
  279. nTypeString = IDS_TYPE_OBJ_TIME_TIMER;
  280. break;
  281. case PERF_100NSEC_TIMER:
  282. nTypeString = IDS_TYPE_100NS_TIMER;
  283. break;
  284. case PERF_100NSEC_TIMER_INV:
  285. nTypeString = IDS_TYPE_100NS_TIMER_INV;
  286. break;
  287. case PERF_COUNTER_MULTI_TIMER:
  288. nTypeString = IDS_TYPE_MULTI_TIMER;
  289. break;
  290. case PERF_COUNTER_MULTI_TIMER_INV:
  291. nTypeString = IDS_TYPE_MULTI_TIMER_INV;
  292. break;
  293. case PERF_COUNTER_MULTI_BASE:
  294. nTypeString = IDS_TYPE_MULTI_BASE;
  295. break;
  296. case PERF_100NSEC_MULTI_TIMER:
  297. nTypeString = IDS_TYPE_100NS_MULTI_TIMER;
  298. break;
  299. case PERF_100NSEC_MULTI_TIMER_INV:
  300. nTypeString = IDS_TYPE_100NS_MULTI_TIMER_INV;
  301. break;
  302. case PERF_RAW_FRACTION:
  303. nTypeString = IDS_TYPE_RAW_FRACTION;
  304. break;
  305. case PERF_LARGE_RAW_FRACTION:
  306. nTypeString = IDS_TYPE_LARGE_RAW_FRACTION;
  307. break;
  308. case PERF_RAW_BASE:
  309. nTypeString = IDS_TYPE_RAW_BASE;
  310. break;
  311. case PERF_LARGE_RAW_BASE:
  312. nTypeString = IDS_TYPE_LARGE_RAW_BASE;
  313. break;
  314. case PERF_ELAPSED_TIME:
  315. nTypeString = IDS_TYPE_ELAPSED_TIME;
  316. break;
  317. case PERF_COUNTER_HISTOGRAM_TYPE:
  318. nTypeString = IDS_TYPE_HISTOGRAM;
  319. break;
  320. case PERF_COUNTER_DELTA:
  321. nTypeString = IDS_TYPE_COUNTER_DELTA;
  322. break;
  323. case PERF_COUNTER_LARGE_DELTA:
  324. nTypeString = IDS_TYPE_COUNTER_LARGE_DELTA;
  325. break;
  326. case PERF_PRECISION_SYSTEM_TIMER:
  327. nTypeString = IDS_TYPE_PRECISION_SYSTEM_TIMER;
  328. break;
  329. case PERF_PRECISION_100NS_TIMER:
  330. nTypeString = IDS_TYPE_PRECISION_100NS_TIMER;
  331. break;
  332. case PERF_PRECISION_OBJECT_TIMER:
  333. nTypeString = IDS_TYPE_PRECISION_OBJECT_TIMER;
  334. break;
  335. default:
  336. nTypeString = 0;
  337. break;
  338. }
  339. if (nTypeString != 0) {
  340. szReturn = GetStringResource(NULL, nTypeString);
  341. }
  342. if (szReturn == NULL) {
  343. szReturn = cszEmptyString;
  344. }
  345. return szReturn;
  346. }
  347. BOOL
  348. ShowCounterData(
  349. HWND hDlg,
  350. LONG lDisplayIndex
  351. )
  352. {
  353. PPERF_OBJECT_TYPE pObject;
  354. PPERF_COUNTER_DEFINITION pCounterDef;
  355. PPERF_COUNTER_BLOCK pCounterBlock;
  356. UINT nSelObject, nSelInstance;
  357. WCHAR szTypeNameBuffer[MAX_PATH];
  358. WCHAR szDisplayBuffer[SMALL_BUFFER_SIZE];
  359. DWORD * pdwLoDword, * pdwHiDword;
  360. DWORD dwThisCounter;
  361. HRESULT hError;
  362. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_RESETCONTENT, 0, 0);
  363. nSelObject = (UINT) SendDlgItemMessageW(hDlg, IDC_OBJECT, CB_GETCURSEL, 0, 0);
  364. nSelInstance = (UINT) SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_GETCURSEL, 0, 0);
  365. if ((nSelObject != CB_ERR) && (nSelInstance != CB_ERR)) {
  366. pObject = (PPERF_OBJECT_TYPE)
  367. SendDlgItemMessageW(hDlg, IDC_OBJECT, CB_GETITEMDATA, (WPARAM) nSelObject, 0);
  368. pCounterBlock = (PPERF_COUNTER_BLOCK)
  369. SendDlgItemMessageW(hDlg, IDC_INSTANCE, CB_GETITEMDATA, (WPARAM) nSelInstance, 0);
  370. pCounterDef = FirstCounter(pObject);
  371. for (dwThisCounter = 0; dwThisCounter < pObject->NumCounters; dwThisCounter ++) {
  372. // get pointer to this counter's data (in this instance if applicable
  373. pdwLoDword = (PDWORD) ((LPBYTE) pCounterBlock + pCounterDef->CounterOffset);
  374. pdwHiDword = pdwLoDword + 1;
  375. hError = StringCchCopyW(szTypeNameBuffer,
  376. RTL_NUMBER_OF(szTypeNameBuffer),
  377. GetCounterTypeName(pCounterDef->CounterType));
  378. if (* szTypeNameBuffer == L'\0') {
  379. // no string returned so format data as HEX DWORD
  380. hError = StringCchPrintfW(szTypeNameBuffer, RTL_NUMBER_OF(szTypeNameBuffer), L"Undefined Type: 0x%8.8x",
  381. pCounterDef->CounterType);
  382. }
  383. if (pCounterDef->CounterSize <= sizeof(DWORD)) {
  384. hError = StringCchPrintfW(szDisplayBuffer, RTL_NUMBER_OF(szDisplayBuffer),
  385. L"%d\t%ws\t%ws\t0x%8.8x (%d)",
  386. pCounterDef->CounterNameTitleIndex,
  387. pCounterDef->CounterNameTitleIndex <= dwLastName ?
  388. szNameTable[pCounterDef->CounterNameTitleIndex] : L"Name not loaded",
  389. szTypeNameBuffer,
  390. * pdwLoDword, * pdwLoDword);
  391. }
  392. else {
  393. hError = StringCchPrintfW(szDisplayBuffer, RTL_NUMBER_OF(szDisplayBuffer),
  394. L"%d\t%ws\t%ws\t0x%8.8x%8.8x",
  395. pCounterDef->CounterNameTitleIndex,
  396. pCounterDef->CounterNameTitleIndex <= dwLastName ?
  397. szNameTable[pCounterDef->CounterNameTitleIndex] : L"Name not loaded",
  398. szTypeNameBuffer,
  399. * pdwHiDword, * pdwLoDword);
  400. }
  401. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_INSERTSTRING, (WPARAM) -1, (LPARAM) szDisplayBuffer);
  402. pCounterDef = NextCounter(pCounterDef);
  403. }
  404. if (lDisplayIndex < 0) {
  405. if (pObject->DefaultCounter >= 0) {
  406. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_SETCURSEL, (WPARAM) pObject->DefaultCounter, 0);
  407. }
  408. else {
  409. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_SETCURSEL, (WPARAM) 0, 0);
  410. }
  411. }
  412. else {
  413. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_SETCURSEL, (WPARAM) lDisplayIndex, (LPARAM) 0);
  414. }
  415. }
  416. else {
  417. // no object and/or instsance selected so nothing else to do
  418. }
  419. return TRUE;
  420. }
  421. BOOL
  422. OnComputerChange(
  423. HWND hDlg
  424. )
  425. {
  426. WCHAR szLocalComputerName[MAX_COMPUTERNAME_LENGTH + 3];
  427. HKEY hLocalMachineKey = NULL;
  428. HKEY hLocalPerfKey = NULL;
  429. LPWSTR *szLocalNameTable = NULL;
  430. BOOL bResult = FALSE;
  431. HWND hWndComputerName;
  432. HRESULT hError;
  433. SET_WAIT_CURSOR;
  434. // get name from edit control
  435. hWndComputerName = GetDlgItem(hDlg, IDC_COMPUTERNAME);
  436. GetWindowTextW(hWndComputerName, szLocalComputerName, MAX_COMPUTERNAME_LENGTH + 2);
  437. if (lstrcmpiW(szComputerName, szLocalComputerName) != 0) {
  438. // a new name has been entered so try to connect to it
  439. if (lstrcmpiW(szLocalComputerName, szThisComputerName) == 0) {
  440. // then this is the local machine which is a special case
  441. hLocalMachineKey = HKEY_LOCAL_MACHINE;
  442. hLocalPerfKey = HKEY_PERFORMANCE_DATA;
  443. szLocalComputerName[0] = L'\0';
  444. }
  445. else {
  446. // try to connect to remote computer
  447. if (RegConnectRegistryW(szLocalComputerName, HKEY_LOCAL_MACHINE, & hLocalMachineKey)
  448. == ERROR_SUCCESS) {
  449. // connected to the new machine, so Try to connect to
  450. // the performance data, too
  451. if (RegConnectRegistryW(szLocalComputerName, HKEY_PERFORMANCE_DATA, & hLocalPerfKey)
  452. != ERROR_SUCCESS) {
  453. DisplayMessageBox(hDlg, IDS_UNABLE_CONNECT_PERF, IDS_APP_ERROR, MB_OK);
  454. }
  455. }
  456. else {
  457. DisplayMessageBox(hDlg, IDS_UNABLE_CONNECT_MACH, IDS_APP_ERROR, MB_OK);
  458. }
  459. }
  460. if ((hLocalMachineKey != NULL) && (hLocalPerfKey != NULL)) {
  461. // try to get a new name table
  462. szLocalNameTable = BuildNameTable(
  463. (szLocalComputerName == NULL ? NULL : szLocalComputerName), NULL, & dwLastName);
  464. if (szLocalNameTable != NULL) {
  465. bResult = TRUE;
  466. }
  467. else {
  468. DisplayMessageBox(hDlg, IDS_UNABLE_GET_NAMES, IDS_APP_ERROR, MB_OK);
  469. }
  470. }
  471. if (bResult) {
  472. // made it so close the old connections
  473. if (hKeyMachine != NULL && hKeyMachine != HKEY_LOCAL_MACHINE) {
  474. RegCloseKey(hKeyMachine);
  475. }
  476. hKeyMachine = hLocalMachineKey;
  477. if (hKeyPerformance != NULL && hKeyPerformance != HKEY_PERFORMANCE_DATA) {
  478. RegCloseKey(hKeyPerformance);
  479. }
  480. hKeyPerformance = hLocalPerfKey;
  481. MemoryFree(szNameTable);
  482. szNameTable = szLocalNameTable;
  483. if (szLocalComputerName[0] == L'\0') {
  484. hError = StringCchCopyW(szComputerName, MAX_COMPUTERNAME_LENGTH + 3, szThisComputerName);
  485. }
  486. // then update the fields
  487. bResult = LoadObjectList(hDlg, NULL);
  488. if (bResult) {
  489. LoadInstanceList(hDlg, NULL);
  490. ShowCounterData(hDlg, -1);
  491. }
  492. }
  493. else {
  494. // unable to get info from machine so clean up
  495. if (hLocalPerfKey != NULL && hLocalPerfKey != HKEY_PERFORMANCE_DATA) RegCloseKey(hLocalPerfKey);
  496. if (hLocalMachineKey != NULL && hLocalMachineKey != HKEY_LOCAL_MACHINE) RegCloseKey(hLocalMachineKey);
  497. MemoryFree(szLocalNameTable);
  498. // reset computer name to the one that works.
  499. SetWindowTextW(hWndComputerName, szComputerName);
  500. }
  501. }
  502. else {
  503. // the name hasn't changed
  504. }
  505. return TRUE;
  506. }
  507. BOOL
  508. MainDlg_WM_INITDIALOG(
  509. HWND hDlg,
  510. WPARAM wParam,
  511. LPARAM lParam
  512. )
  513. {
  514. DWORD dwComputerNameLength = MAX_COMPUTERNAME_LENGTH + 1;
  515. HRESULT hError;
  516. UNREFERENCED_PARAMETER(lParam);
  517. UNREFERENCED_PARAMETER(wParam);
  518. SET_WAIT_CURSOR;
  519. hError = StringCchCopyW(szThisComputerName, MAX_COMPUTERNAME_LENGTH + 3, L"\\\\");
  520. GetComputerNameW(szThisComputerName + 2, & dwComputerNameLength);
  521. szComputerName[0] = L'\0'; // reset the computer name
  522. // load the local machine name into the edit box
  523. SetWindowTextW(GetDlgItem(hDlg, IDC_COMPUTERNAME), szThisComputerName);
  524. SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_SETTABSTOPS, (WPARAM) NUM_TAB_STOPS, (LPARAM) & nDataListTabs);
  525. CheckDlgButton(hDlg, IDC_INCLUDE_COSTLY, UNCHECKED);
  526. OnComputerChange(hDlg);
  527. SetFocus(GetDlgItem(hDlg, IDC_OBJECT));
  528. SET_ARROW_CURSOR;
  529. return FALSE;
  530. }
  531. BOOL
  532. MainDlg_IDC_COMPUTERNAME(
  533. HWND hDlg,
  534. WORD wNotifyMsg,
  535. HWND hWndControl
  536. )
  537. {
  538. BOOL bReturn = FALSE;
  539. UNREFERENCED_PARAMETER(hWndControl);
  540. switch (wNotifyMsg) {
  541. case EN_KILLFOCUS:
  542. OnComputerChange(hDlg);
  543. bReturn = TRUE;
  544. break;
  545. default:
  546. bReturn = FALSE;
  547. break;
  548. }
  549. return bReturn;
  550. }
  551. BOOL
  552. MainDlg_IDC_OBJECT(
  553. HWND hDlg,
  554. WORD wNotifyMsg,
  555. HWND hWndControl
  556. )
  557. {
  558. BOOL bReturn = FALSE;
  559. UNREFERENCED_PARAMETER(hWndControl);
  560. switch (wNotifyMsg) {
  561. case CBN_SELCHANGE:
  562. SET_WAIT_CURSOR;
  563. if (pMainPerfData) {
  564. LoadInstanceList(hDlg, NULL);
  565. ShowCounterData(hDlg, -1);
  566. }
  567. SET_ARROW_CURSOR;
  568. bReturn = TRUE;
  569. break;
  570. default:
  571. bReturn = FALSE;
  572. break;
  573. }
  574. return bReturn;
  575. }
  576. BOOL
  577. MainDlg_IDC_INSTANCE(
  578. HWND hDlg,
  579. WORD wNotifyMsg,
  580. HWND hWndControl
  581. )
  582. {
  583. BOOL bReturn = FALSE;
  584. UNREFERENCED_PARAMETER(hWndControl);
  585. switch (wNotifyMsg) {
  586. case CBN_SELCHANGE:
  587. SET_WAIT_CURSOR;
  588. ShowCounterData(hDlg, -1);
  589. SET_ARROW_CURSOR;
  590. bReturn = TRUE;
  591. break;
  592. default:
  593. bReturn = FALSE;
  594. break;
  595. }
  596. return bReturn;
  597. }
  598. BOOL
  599. MainDlg_IDC_DATA_LIST(
  600. HWND hDlg,
  601. WORD wNotifyMsg,
  602. HWND hWndControl
  603. )
  604. {
  605. UNREFERENCED_PARAMETER(hDlg);
  606. UNREFERENCED_PARAMETER(wNotifyMsg);
  607. UNREFERENCED_PARAMETER(hWndControl);
  608. return (FALSE);
  609. }
  610. BOOL
  611. MainDlg_IDC_REFRESH(
  612. HWND hDlg,
  613. WORD wNotifyMsg,
  614. HWND hWndControl
  615. )
  616. {
  617. WCHAR szSelObject[MAX_PATH + 1];
  618. WCHAR szSelInstance[MAX_PATH + 1];
  619. BOOL bResult;
  620. LONG lCounterIdx;
  621. BOOL bReturn = FALSE;
  622. UNREFERENCED_PARAMETER(hWndControl);
  623. switch (wNotifyMsg) {
  624. case BN_CLICKED:
  625. SET_WAIT_CURSOR;
  626. GetDlgItemTextW(hDlg, IDC_OBJECT, szSelObject, RTL_NUMBER_OF(szSelObject));
  627. GetDlgItemTextW(hDlg, IDC_INSTANCE, szSelInstance, RTL_NUMBER_OF(szSelInstance));
  628. lCounterIdx = (ULONG) SendDlgItemMessageW(hDlg, IDC_DATA_LIST, LB_GETCURSEL, 0, 0);
  629. bResult = LoadObjectList(hDlg, szSelObject);
  630. if (bResult) {
  631. LoadInstanceList(hDlg, szSelInstance);
  632. ShowCounterData(hDlg, lCounterIdx);
  633. }
  634. SET_ARROW_CURSOR;
  635. bReturn = TRUE;
  636. break;
  637. default:
  638. bReturn = FALSE;
  639. break;
  640. }
  641. return bReturn;
  642. }
  643. BOOL
  644. MainDlg_IDC_ABOUT()
  645. {
  646. WCHAR buffer[SMALL_BUFFER_SIZE];
  647. WCHAR strProgram[SMALL_BUFFER_SIZE];
  648. DWORD dw;
  649. LPBYTE pVersionInfo = NULL;
  650. LPWSTR pVersion = NULL;
  651. LPWSTR pProduct = NULL;
  652. LPWSTR pCopyRight = NULL;
  653. HRESULT hError;
  654. ZeroMemory(buffer, sizeof(buffer));
  655. ZeroMemory(strProgram, sizeof(strProgram));
  656. dw = GetModuleFileNameW(NULL, strProgram, RTL_NUMBER_OF(strProgram) - 1);
  657. if(dw > 0) {
  658. dw = GetFileVersionInfoSizeW(strProgram, & dw);
  659. if (dw > 0) {
  660. pVersionInfo = (LPBYTE) MemoryAllocate(dw);
  661. if(NULL != pVersionInfo) {
  662. if (GetFileVersionInfoW(strProgram, 0, dw, pVersionInfo)) {
  663. LPDWORD lptr = NULL;
  664. VerQueryValueW(pVersionInfo, L"\\VarFileInfo\\Translation", (void **) & lptr, (UINT *) & dw);
  665. if (lptr != NULL) {
  666. hError = StringCchPrintfW(buffer, RTL_NUMBER_OF(buffer),
  667. L"\\StringFileInfo\\%04x%04x\\ProductVersion",
  668. LOWORD(* lptr), HIWORD(* lptr));
  669. VerQueryValueW(pVersionInfo, buffer, (void **) & pVersion, (UINT *) & dw);
  670. hError = StringCchPrintfW(buffer, RTL_NUMBER_OF(buffer),
  671. L"\\StringFileInfo\\%04x%04x\\OriginalFilename",
  672. LOWORD(* lptr), HIWORD(* lptr));
  673. VerQueryValueW(pVersionInfo, buffer, (void **) & pProduct, (UINT *) & dw);
  674. hError = StringCchPrintfW(buffer, RTL_NUMBER_OF(buffer),
  675. L"\\StringFileInfo\\%04x%04x\\LegalCopyright",
  676. LOWORD(* lptr), HIWORD(* lptr));
  677. VerQueryValueW(pVersionInfo, buffer, (void **) & pCopyRight, (UINT *) & dw);
  678. }
  679. if(pProduct != NULL && pVersion != NULL && pCopyRight != NULL) {
  680. hError = StringCchPrintfW(buffer, RTL_NUMBER_OF(buffer),
  681. L"\nMicrosoft (R) %ws\nVersion: %ws\n%ws",
  682. pProduct, pVersion, pCopyRight);
  683. }
  684. }
  685. MemoryFree(pVersionInfo);
  686. }
  687. }
  688. }
  689. MessageBoxW(NULL, buffer, L"About ShowPerf", MB_OK);
  690. return TRUE;
  691. }
  692. BOOL
  693. MainDlg_WM_COMMAND(
  694. HWND hDlg,
  695. WPARAM wParam,
  696. LPARAM lParam
  697. )
  698. {
  699. WORD wCtrlId = GET_CONTROL_ID(wParam);
  700. WORD wNotifyMsg = GET_NOTIFY_MSG(wParam, lParam);
  701. HWND hWndControl = GET_COMMAND_WND(lParam);
  702. BOOL bReturn = FALSE;
  703. switch (wCtrlId) {
  704. case IDC_COMPUTERNAME:
  705. bReturn = MainDlg_IDC_COMPUTERNAME(hDlg, wNotifyMsg, hWndControl);
  706. break;
  707. case IDC_OBJECT:
  708. bReturn = MainDlg_IDC_OBJECT(hDlg, wNotifyMsg, hWndControl);
  709. break;
  710. case IDC_INSTANCE:
  711. bReturn = MainDlg_IDC_INSTANCE(hDlg, wNotifyMsg, hWndControl);
  712. break;
  713. case IDC_DATA_LIST:
  714. bReturn = MainDlg_IDC_DATA_LIST(hDlg, wNotifyMsg, hWndControl);
  715. break;
  716. case IDC_REFRESH:
  717. bReturn = MainDlg_IDC_REFRESH(hDlg, wNotifyMsg, hWndControl);
  718. break;
  719. case IDC_ABOUT:
  720. bReturn = MainDlg_IDC_ABOUT();
  721. break;
  722. case IDOK:
  723. EndDialog(hDlg, IDOK);
  724. bReturn = TRUE;
  725. break;
  726. default:
  727. bReturn = FALSE;
  728. break;
  729. }
  730. return bReturn;
  731. }
  732. BOOL
  733. MainDlg_WM_SYSCOMMAND(
  734. HWND hDlg,
  735. WPARAM wParam,
  736. LPARAM lParam
  737. )
  738. {
  739. BOOL bReturn = FALSE;
  740. UNREFERENCED_PARAMETER(lParam);
  741. switch (wParam) {
  742. case SC_CLOSE:
  743. EndDialog(hDlg, IDOK);
  744. bReturn = TRUE;
  745. break;
  746. default:
  747. bReturn = FALSE;
  748. break;
  749. }
  750. return bReturn;
  751. }
  752. BOOL
  753. MainDlg_WM_CLOSE(
  754. HWND hDlg,
  755. WPARAM wParam,
  756. LPARAM lParam
  757. )
  758. {
  759. UNREFERENCED_PARAMETER(lParam);
  760. UNREFERENCED_PARAMETER(wParam);
  761. UNREFERENCED_PARAMETER(hDlg);
  762. MemoryFree(pMainPerfData);
  763. pMainPerfData = NULL;
  764. MemoryFree(szNameTable);
  765. szNameTable = NULL;
  766. return TRUE;
  767. }
  768. INT_PTR
  769. MainDlgProc(
  770. HWND hDlg,
  771. UINT message,
  772. WPARAM wParam,
  773. LPARAM lParam
  774. )
  775. {
  776. BOOL bReturn = FALSE;
  777. switch (message) {
  778. case WM_INITDIALOG:
  779. bReturn = MainDlg_WM_INITDIALOG(hDlg, wParam, lParam);
  780. break;
  781. case WM_COMMAND:
  782. bReturn = MainDlg_WM_COMMAND(hDlg, wParam, lParam);
  783. break;
  784. case WM_SYSCOMMAND:
  785. bReturn = MainDlg_WM_SYSCOMMAND(hDlg, wParam, lParam);
  786. break;
  787. case WM_CLOSE:
  788. bReturn = MainDlg_WM_CLOSE(hDlg, wParam, lParam);
  789. break;
  790. default:
  791. bReturn = FALSE;
  792. break;
  793. }
  794. return bReturn;
  795. }