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.

369 lines
13 KiB

  1. // PdhPathTestDialog.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "assert.h"
  5. #include "dph_test.h"
  6. #include "PdhPathTestDialog.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CPdhPathTestDialog dialog
  14. CPdhPathTestDialog::CPdhPathTestDialog(CWnd* pParent /*=NULL*/)
  15. : CDialog(CPdhPathTestDialog::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CPdhPathTestDialog)
  18. m_CounterName = _T("");
  19. m_FullPathString = _T("");
  20. m_IncludeInstanceName = FALSE;
  21. m_InstanceNameString = _T("");
  22. m_IncludeMachineName = FALSE;
  23. m_ObjectNameString = _T("");
  24. m_MachineNameString = _T("");
  25. m_UnicodeFunctions = FALSE;
  26. m_WbemOutput = FALSE;
  27. m_WbemInput = FALSE;
  28. //}}AFX_DATA_INIT
  29. }
  30. void CPdhPathTestDialog::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CPdhPathTestDialog)
  34. DDX_Text(pDX, IDC_COUNTER_NAME_EDIT, m_CounterName);
  35. DDV_MaxChars(pDX, m_CounterName, 255);
  36. DDX_Text(pDX, IDC_FULL_PATH_STRING_EDIT, m_FullPathString);
  37. DDV_MaxChars(pDX, m_FullPathString, 255);
  38. DDX_Check(pDX, IDC_INSTANCE_NAME_CHK, m_IncludeInstanceName);
  39. DDX_Text(pDX, IDC_INSTANCE_NAME_EDIT, m_InstanceNameString);
  40. DDV_MaxChars(pDX, m_InstanceNameString, 255);
  41. DDX_Check(pDX, IDC_MACHINE_NAME_CHK, m_IncludeMachineName);
  42. DDX_Text(pDX, IDC_OBJECT_NAME_EDIT, m_ObjectNameString);
  43. DDV_MaxChars(pDX, m_ObjectNameString, 255);
  44. DDX_Text(pDX, IDC_MACHINE_NAME_EDIT, m_MachineNameString);
  45. DDV_MaxChars(pDX, m_MachineNameString, 255);
  46. DDX_Check(pDX, IDC_UNICODE_CHK, m_UnicodeFunctions);
  47. DDX_Check(pDX, IDC_WBEM_OUTPUT_CHK, m_WbemOutput);
  48. DDX_Check(pDX, IDC_WBEM_INPUT_CHK, m_WbemInput);
  49. //}}AFX_DATA_MAP
  50. }
  51. BEGIN_MESSAGE_MAP(CPdhPathTestDialog, CDialog)
  52. //{{AFX_MSG_MAP(CPdhPathTestDialog)
  53. ON_BN_CLICKED(IDC_MACHINE_NAME_CHK, OnMachineNameChk)
  54. ON_BN_CLICKED(IDC_INSTANCE_NAME_CHK, OnInstanceNameChk)
  55. ON_BN_CLICKED(IDC_PROCESS_BTN, OnProcessBtn)
  56. ON_BN_CLICKED(IDC_ENTER_ELEM_BTN, OnEnterElemBtn)
  57. ON_BN_CLICKED(IDC_ENTER_PATH_BTN, OnEnterPathBtn)
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. void CPdhPathTestDialog::SetDialogMode()
  61. {
  62. // reads the mode of the dialog box and enables the appropriate controls
  63. BOOL bEnterComponents;
  64. if (((CButton *)GetDlgItem(IDC_ENTER_ELEM_BTN))->GetCheck() == 0) {
  65. bEnterComponents = FALSE;
  66. } else {
  67. bEnterComponents = TRUE;
  68. }
  69. // enabled for component entries
  70. GetDlgItem(IDC_MACHINE_NAME_EDIT)->EnableWindow(bEnterComponents);
  71. GetDlgItem(IDC_MACHINE_NAME_CHK)->EnableWindow(bEnterComponents);
  72. GetDlgItem(IDC_OBJECT_NAME_EDIT)->EnableWindow(bEnterComponents);
  73. GetDlgItem(IDC_INSTANCE_NAME_CHK)->EnableWindow(bEnterComponents);
  74. GetDlgItem(IDC_INSTANCE_NAME_EDIT)->EnableWindow(bEnterComponents);
  75. GetDlgItem(IDC_COUNTER_NAME_EDIT)->EnableWindow(bEnterComponents);
  76. if (bEnterComponents) {
  77. // set the edit box state based on check box selections
  78. if (((CButton *)GetDlgItem(IDC_MACHINE_NAME_CHK))->GetCheck()==0) {
  79. GetDlgItem(IDC_MACHINE_NAME_EDIT)->EnableWindow(FALSE);
  80. }
  81. if (((CButton *)GetDlgItem(IDC_INSTANCE_NAME_CHK))->GetCheck() == 0) {
  82. GetDlgItem(IDC_INSTANCE_NAME_EDIT)->EnableWindow(FALSE);
  83. }
  84. }
  85. // enabled for path string entries
  86. GetDlgItem(IDC_FULL_PATH_STRING_EDIT)->EnableWindow(!bEnterComponents);
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CPdhPathTestDialog message handlers
  90. void CPdhPathTestDialog::OnMachineNameChk()
  91. {
  92. BOOL bMode;
  93. bMode = (((CButton *)GetDlgItem(IDC_MACHINE_NAME_CHK))->GetCheck()==1);
  94. GetDlgItem(IDC_MACHINE_NAME_EDIT)->EnableWindow(bMode);
  95. }
  96. void CPdhPathTestDialog::OnInstanceNameChk()
  97. {
  98. BOOL bMode;
  99. bMode = (((CButton *)GetDlgItem(IDC_INSTANCE_NAME_CHK))->GetCheck() == 1);
  100. GetDlgItem(IDC_INSTANCE_NAME_EDIT)->EnableWindow(bMode);
  101. }
  102. void CPdhPathTestDialog::OnProcessBtn()
  103. {
  104. /*
  105. typedef struct _PDH_COUNTER_PATH_ELEMENTS_A {
  106. LPSTR szMachineName;
  107. LPSTR szObjectName;
  108. LPSTR szInstanceName;
  109. LPSTR szParentInstance;
  110. DWORD dwInstanceIndex;
  111. LPSTR szCounterName;
  112. } PDH_COUNTER_PATH_ELEMENTS_A, *PPDH_COUNTER_PATH_ELEMENTS_A;
  113. */
  114. PDH_COUNTER_PATH_ELEMENTS_A pdhPathElemA;
  115. PDH_COUNTER_PATH_ELEMENTS_W pdhPathElemW;
  116. PPDH_COUNTER_PATH_ELEMENTS_A ppdhPathElemA;
  117. PPDH_COUNTER_PATH_ELEMENTS_W ppdhPathElemW;
  118. PDH_STATUS pdhStatus;
  119. CHAR szRtnBuffer[1024];
  120. WCHAR wszRtnBuffer[1024];
  121. DWORD dwBufSize;
  122. DWORD dwFlags;
  123. BYTE pBuffer[2048];
  124. WCHAR wszMachineName[MAX_PATH];
  125. WCHAR wszObjectName[MAX_PATH];
  126. WCHAR wszInstanceName[MAX_PATH];
  127. WCHAR wszCounterName[MAX_PATH];
  128. UpdateData (TRUE); // get values from Dialog box
  129. // set flags values
  130. dwFlags = 0;
  131. dwFlags |= (m_WbemOutput ? PDH_PATH_WBEM_RESULT : 0);
  132. dwFlags |= (m_WbemInput ? PDH_PATH_WBEM_INPUT : 0);
  133. if (((CButton *)GetDlgItem(IDC_ENTER_ELEM_BTN))->GetCheck() == 1) {
  134. // then read elements and produce path string
  135. if (((CButton *)GetDlgItem(IDC_UNICODE_CHK))->GetCheck() == 0) {
  136. // use ANSI functions
  137. if (m_IncludeMachineName) {
  138. pdhPathElemA.szMachineName = (LPSTR)(LPCSTR)m_MachineNameString;
  139. } else {
  140. pdhPathElemA.szMachineName = NULL;
  141. }
  142. pdhPathElemA.szObjectName = (LPSTR)(LPCSTR)m_ObjectNameString;
  143. if (m_IncludeInstanceName) {
  144. pdhPathElemA.szInstanceName = (LPSTR)(LPCSTR)m_InstanceNameString;
  145. } else {
  146. pdhPathElemA.szInstanceName = NULL;
  147. }
  148. pdhPathElemA.szParentInstance = NULL;
  149. pdhPathElemA.dwInstanceIndex = 0;
  150. pdhPathElemA.szCounterName = (LPSTR)(LPCSTR)m_CounterName;
  151. dwBufSize = sizeof(szRtnBuffer)/sizeof(szRtnBuffer[0]);
  152. pdhStatus = PdhMakeCounterPathA (
  153. &pdhPathElemA,
  154. szRtnBuffer,
  155. &dwBufSize,
  156. dwFlags);
  157. if (pdhStatus != ERROR_SUCCESS) {
  158. // return error in string buffer
  159. sprintf (szRtnBuffer, "PDH Error 0x%8.8x", pdhStatus);
  160. }
  161. m_FullPathString = szRtnBuffer;
  162. } else {
  163. // use unicode functions
  164. if (m_IncludeMachineName) {
  165. mbstowcs (wszMachineName,
  166. (LPCSTR)m_MachineNameString,
  167. m_MachineNameString.GetLength() + 1);
  168. pdhPathElemW.szMachineName = wszMachineName;
  169. } else {
  170. pdhPathElemW.szMachineName = NULL;
  171. }
  172. mbstowcs (wszObjectName,
  173. (LPCSTR)m_ObjectNameString,
  174. m_ObjectNameString.GetLength() + 1);
  175. pdhPathElemW.szObjectName = wszObjectName;
  176. if (m_IncludeInstanceName) {
  177. mbstowcs (wszInstanceName,
  178. (LPCSTR)m_InstanceNameString,
  179. m_InstanceNameString.GetLength() + 1);
  180. pdhPathElemW.szInstanceName = wszInstanceName;
  181. } else {
  182. pdhPathElemW.szInstanceName = NULL;
  183. }
  184. pdhPathElemW.szParentInstance = NULL;
  185. pdhPathElemW.dwInstanceIndex = 0;
  186. mbstowcs (wszCounterName,
  187. (LPCSTR)m_CounterName,
  188. m_CounterName.GetLength() + 1);
  189. pdhPathElemW.szCounterName = wszCounterName;
  190. dwBufSize = sizeof(wszRtnBuffer)/sizeof(wszRtnBuffer[0]);
  191. pdhStatus = PdhMakeCounterPathW (
  192. &pdhPathElemW,
  193. wszRtnBuffer,
  194. &dwBufSize,
  195. dwFlags);
  196. if (pdhStatus != ERROR_SUCCESS) {
  197. // return error in string buffer
  198. sprintf (szRtnBuffer, "PDH Error 0x%8.8x", pdhStatus);
  199. } else {
  200. wcstombs (szRtnBuffer, wszRtnBuffer, lstrlenW(wszRtnBuffer)+1);
  201. }
  202. m_FullPathString = szRtnBuffer;
  203. }
  204. } else {
  205. // read path string and produce elements
  206. if (((CButton *)GetDlgItem(IDC_UNICODE_CHK))->GetCheck() == 0) {
  207. // use ANSI functions
  208. dwBufSize = sizeof(pBuffer);
  209. pdhStatus = PdhParseCounterPathA (
  210. (LPCSTR)m_FullPathString,
  211. (PDH_COUNTER_PATH_ELEMENTS_A *)&pBuffer[0],
  212. &dwBufSize,
  213. dwFlags);
  214. if (pdhStatus != ERROR_SUCCESS) {
  215. // return error in string buffer
  216. sprintf (szRtnBuffer, "PDH Error 0x%8.8x", pdhStatus);
  217. m_MachineNameString = szRtnBuffer;
  218. m_ObjectNameString = "";
  219. m_InstanceNameString = "";
  220. m_CounterName = "";
  221. } else {
  222. // update fields
  223. ppdhPathElemA = (PDH_COUNTER_PATH_ELEMENTS_A *)&pBuffer[0];
  224. if (ppdhPathElemA->szMachineName != NULL) {
  225. m_MachineNameString = ppdhPathElemA->szMachineName;
  226. m_IncludeMachineName = TRUE;
  227. } else {
  228. m_MachineNameString = "";
  229. m_IncludeMachineName = FALSE;
  230. }
  231. assert (ppdhPathElemA->szObjectName != NULL);
  232. m_ObjectNameString = ppdhPathElemA->szObjectName;
  233. if (ppdhPathElemA->szInstanceName != NULL) {
  234. m_InstanceNameString = ppdhPathElemA->szInstanceName;
  235. m_IncludeInstanceName = TRUE;
  236. } else {
  237. m_InstanceNameString = "";
  238. m_IncludeInstanceName = FALSE;
  239. }
  240. assert (ppdhPathElemA->szCounterName != NULL);
  241. m_CounterName = ppdhPathElemA->szCounterName;
  242. }
  243. } else {
  244. // do unicode functions
  245. dwBufSize = sizeof(pBuffer);
  246. mbstowcs (wszRtnBuffer,
  247. (LPCSTR)m_FullPathString,
  248. m_FullPathString.GetLength());
  249. pdhStatus = PdhParseCounterPathW (
  250. wszRtnBuffer,
  251. (PDH_COUNTER_PATH_ELEMENTS_W *)&pBuffer[0],
  252. &dwBufSize,
  253. dwFlags);
  254. if (pdhStatus != ERROR_SUCCESS) {
  255. // return error in string buffer
  256. sprintf (szRtnBuffer, "PDH Error 0x%8.8x", pdhStatus);
  257. m_MachineNameString = szRtnBuffer;
  258. m_ObjectNameString = "";
  259. m_InstanceNameString = "";
  260. m_CounterName = "";
  261. } else {
  262. // update fields
  263. ppdhPathElemW = (PDH_COUNTER_PATH_ELEMENTS_W *)&pBuffer[0];
  264. if (ppdhPathElemW->szMachineName != NULL) {
  265. wcstombs (szRtnBuffer, ppdhPathElemW->szMachineName,
  266. lstrlenW(ppdhPathElemW->szMachineName)+1);
  267. m_MachineNameString = szRtnBuffer;
  268. m_IncludeMachineName = TRUE;
  269. } else {
  270. m_MachineNameString = "";
  271. m_IncludeMachineName = FALSE;
  272. }
  273. assert (ppdhPathElemW->szObjectName != NULL);
  274. wcstombs (szRtnBuffer, ppdhPathElemW->szObjectName,
  275. lstrlenW(ppdhPathElemW->szObjectName)+1);
  276. m_ObjectNameString = szRtnBuffer;
  277. if (ppdhPathElemW->szInstanceName != NULL) {
  278. wcstombs (szRtnBuffer, ppdhPathElemW->szInstanceName,
  279. lstrlenW(ppdhPathElemW->szInstanceName)+1);
  280. m_InstanceNameString = szRtnBuffer;
  281. m_IncludeInstanceName = TRUE;
  282. } else {
  283. m_InstanceNameString = "";
  284. m_IncludeInstanceName = FALSE;
  285. }
  286. assert (ppdhPathElemA->szCounterName != NULL);
  287. wcstombs (szRtnBuffer, ppdhPathElemW->szCounterName,
  288. lstrlenW(ppdhPathElemW->szCounterName)+1);
  289. m_CounterName = szRtnBuffer;
  290. }
  291. }
  292. }
  293. UpdateData (FALSE); // put values back into Dialog box
  294. }
  295. BOOL CPdhPathTestDialog::OnInitDialog()
  296. {
  297. CDialog::OnInitDialog();
  298. UpdateData (FALSE); // update controls
  299. ((CButton *)GetDlgItem(IDC_ENTER_ELEM_BTN))->SetCheck(TRUE);
  300. ((CButton *)GetDlgItem(IDC_ENTER_PATH_BTN))->SetCheck(FALSE);
  301. SetDialogMode();
  302. // force unicode functions for now
  303. ((CButton *)GetDlgItem(IDC_UNICODE_CHK))->SetCheck(TRUE);
  304. return TRUE; // return TRUE unless you set the focus to a control
  305. // EXCEPTION: OCX Property Pages should return FALSE
  306. }
  307. void CPdhPathTestDialog::OnEnterElemBtn()
  308. {
  309. SetDialogMode();
  310. }
  311. void CPdhPathTestDialog::OnEnterPathBtn()
  312. {
  313. SetDialogMode();
  314. }