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.

696 lines
21 KiB

  1. /*++
  2. Copyright (C) 1995-1999 Microsoft Corporation
  3. Module Name:
  4. datasrc.c
  5. Abstract:
  6. data source selection dialog box functions
  7. Revision History
  8. Bob Watson (a-robw) Feb-95 Created
  9. --*/
  10. #include <windows.h>
  11. #include <assert.h>
  12. #include <tchar.h>
  13. #include "mbctype.h"
  14. #include <pdh.h>
  15. #include "pdhidef.h"
  16. #include "pdhdlgs.h"
  17. #include "datasrc.h"
  18. #include "pdhmsg.h"
  19. #include "strings.h"
  20. //
  21. // Constants used in this module
  22. //
  23. ULONG
  24. PdhiDatasrcaulControlIdToHelpIdMap[] =
  25. {
  26. IDC_CURRENT_ACTIVITY, IDH_CURRENT_ACTIVITY,
  27. IDC_DATA_FROM_LOG_FILE, IDH_DATA_FROM_LOG_FILE,
  28. IDC_LOG_FILE_EDIT, IDH_LOG_FILE_EDIT,
  29. 0,0
  30. };
  31. STATIC_BOOL
  32. DataSrcDlg_RadioButton (
  33. IN HWND hDlg,
  34. IN WORD wNotifyMsg,
  35. IN WORD wCtrlId
  36. )
  37. {
  38. int nShowEdit = FALSE;
  39. int nShowBrowseBtn = FALSE;
  40. int nShowRegBtn = FALSE;
  41. int nShowWbemBtn = FALSE;
  42. switch (wNotifyMsg) {
  43. case BN_CLICKED:
  44. switch (wCtrlId) {
  45. case IDC_CURRENT_ACTIVITY:
  46. nShowEdit = FALSE;
  47. nShowBrowseBtn = FALSE;
  48. nShowRegBtn = TRUE;
  49. nShowWbemBtn = TRUE;
  50. break;
  51. case IDC_DATA_FROM_LOG_FILE:
  52. nShowEdit = TRUE;
  53. nShowBrowseBtn = TRUE;
  54. nShowRegBtn = FALSE;
  55. nShowWbemBtn = FALSE;
  56. break;
  57. case IDC_PERF_REG:
  58. case IDC_WBEM_NS:
  59. return TRUE;
  60. }
  61. EnableWindow (GetDlgItem (hDlg, IDC_LOG_FILE_EDIT), nShowEdit);
  62. EnableWindow (GetDlgItem (hDlg, IDC_BROWSE_LOG_FILES), nShowBrowseBtn);
  63. EnableWindow (GetDlgItem (hDlg, IDC_PERF_REG), nShowRegBtn);
  64. EnableWindow (GetDlgItem (hDlg, IDC_WBEM_NS), nShowWbemBtn);
  65. return TRUE;
  66. default:
  67. return FALSE;
  68. }
  69. }
  70. STATIC_BOOL
  71. DataSrcDlg_BROWSE_LOG_FILES (
  72. IN HWND hDlg,
  73. IN WORD wNotifyMsg,
  74. IN HWND hWndControl
  75. )
  76. {
  77. WCHAR szEditBoxString[SMALL_BUFFER_SIZE];
  78. DWORD cchStringLen = SMALL_BUFFER_SIZE;
  79. UNREFERENCED_PARAMETER (hWndControl);
  80. switch (wNotifyMsg) {
  81. case BN_CLICKED:
  82. // get the current filename
  83. SendDlgItemMessageW (hDlg, IDC_LOG_FILE_EDIT, WM_GETTEXT,
  84. (WPARAM)SMALL_BUFFER_SIZE, (LPARAM)szEditBoxString);
  85. if (PdhiBrowseDataSource (hDlg, szEditBoxString, &cchStringLen, TRUE)) {
  86. // then update the edit box and set focus to it.
  87. SendDlgItemMessageW (hDlg, IDC_LOG_FILE_EDIT, WM_SETTEXT,
  88. (WPARAM)0, (LPARAM)szEditBoxString);
  89. }
  90. return TRUE;
  91. default:
  92. return FALSE;
  93. }
  94. }
  95. STATIC_BOOL
  96. DataSrcDlg_OK (
  97. IN HWND hDlg,
  98. IN WORD wNotifyMsg,
  99. IN HWND hWndControl
  100. )
  101. {
  102. PPDHI_DATA_SOURCE_INFO pInfo;
  103. HCURSOR hOldCursor;
  104. DWORD dwFileNameLength;
  105. LPWSTR szStringPtr;
  106. UNREFERENCED_PARAMETER (hWndControl);
  107. switch (wNotifyMsg) {
  108. case BN_CLICKED:
  109. pInfo = (PPDHI_DATA_SOURCE_INFO)GetWindowLongPtrW (hDlg, DWLP_USER);
  110. if (pInfo != NULL) {
  111. hOldCursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
  112. // get data from dialog box
  113. if (IsDlgButtonChecked (hDlg, IDC_CURRENT_ACTIVITY) == 1) {
  114. if (IsDlgButtonChecked (hDlg, IDC_WBEM_NS) == 1) {
  115. // then a WBEM Name Space is selected so get the name
  116. pInfo->dwFlags = PDHI_DATA_SOURCE_WBEM_NAMESPACE;
  117. dwFileNameLength = lstrlenW (cszWMI);
  118. if (dwFileNameLength < pInfo->cchBufferLength) {
  119. szStringPtr = pInfo->szDataSourceFile;
  120. lstrcpyW (pInfo->szDataSourceFile, cszWMI);
  121. pInfo->cchBufferLength = dwFileNameLength;
  122. } else {
  123. // buffer is too small for the file name
  124. // so return the required size but no string
  125. *pInfo->szDataSourceFile = 0;
  126. pInfo->cchBufferLength = dwFileNameLength;
  127. }
  128. } else if (IsDlgButtonChecked (hDlg, IDC_PERF_REG) == 1) {
  129. // then current activity is selected so set flags
  130. pInfo->dwFlags = PDHI_DATA_SOURCE_CURRENT_ACTIVITY;
  131. *pInfo->szDataSourceFile = 0;
  132. pInfo->cchBufferLength = 0;
  133. } else {
  134. assert (FALSE); // no button is pressed.
  135. }
  136. } else if (IsDlgButtonChecked (hDlg, IDC_DATA_FROM_LOG_FILE) == 1) {
  137. // then a log file is selected so get the log file name
  138. pInfo->dwFlags = PDHI_DATA_SOURCE_LOG_FILE;
  139. dwFileNameLength = (DWORD)SendDlgItemMessageW (hDlg,
  140. IDC_LOG_FILE_EDIT, WM_GETTEXTLENGTH, 0, 0);
  141. if (dwFileNameLength < pInfo->cchBufferLength) {
  142. pInfo->cchBufferLength = (DWORD)SendDlgItemMessageW (hDlg,
  143. IDC_LOG_FILE_EDIT, WM_GETTEXT,
  144. (WPARAM)pInfo->cchBufferLength,
  145. (LPARAM)pInfo->szDataSourceFile);
  146. } else {
  147. // buffer is too small for the file name
  148. // so return the required size but no string
  149. *pInfo->szDataSourceFile = 0;
  150. pInfo->cchBufferLength = dwFileNameLength;
  151. }
  152. }
  153. SetCursor (hOldCursor);
  154. EndDialog (hDlg, IDOK);
  155. } else {
  156. // unable to locate data block so no data can be returned.
  157. EndDialog (hDlg, IDCANCEL);
  158. }
  159. return TRUE;
  160. default:
  161. return FALSE;
  162. }
  163. }
  164. STATIC_BOOL
  165. DataSrcDlg_CANCEL (
  166. IN HWND hDlg,
  167. IN WORD wNotifyMsg,
  168. IN HWND hWndControl
  169. )
  170. {
  171. UNREFERENCED_PARAMETER (hWndControl);
  172. switch (wNotifyMsg) {
  173. case BN_CLICKED:
  174. EndDialog (hDlg, IDCANCEL);
  175. return TRUE;
  176. default:
  177. return FALSE;
  178. }
  179. }
  180. STATIC_BOOL
  181. DataSrcDlg_HELP_BTN (
  182. IN HWND hDlg,
  183. IN WORD wNotifyMsg,
  184. IN HWND hWndControl
  185. )
  186. {
  187. UNREFERENCED_PARAMETER (hDlg);
  188. UNREFERENCED_PARAMETER (wNotifyMsg);
  189. UNREFERENCED_PARAMETER (hWndControl);
  190. return FALSE;
  191. }
  192. STATIC_BOOL
  193. DataSrcDlg_WM_INITDIALOG (
  194. IN HWND hDlg,
  195. IN WPARAM wParam,
  196. IN LPARAM lParam
  197. )
  198. {
  199. // LPARAM is the pointer to the structure used for the data source info
  200. BOOL bReturn = TRUE;
  201. PPDHI_DATA_SOURCE_INFO pInfo;
  202. HCURSOR hOldCursor;
  203. int nButton;
  204. int nShowEdit;
  205. int nShowBrowse;
  206. int nShowRegBtn;
  207. int nShowWbemBtn;
  208. HWND hwndFocus;
  209. LPWSTR szDisplayString;
  210. UNREFERENCED_PARAMETER (wParam);
  211. hOldCursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
  212. // must have a pointer to the information structure in the LPARAM
  213. if (lParam == 0) {
  214. SetLastError (PDH_INVALID_ARGUMENT);
  215. EndDialog (hDlg, IDCANCEL);
  216. goto INIT_EXIT;
  217. }
  218. pInfo = (PPDHI_DATA_SOURCE_INFO)lParam;
  219. SetWindowLongPtrW (hDlg, DWLP_USER, (LONG_PTR)pInfo);
  220. // initialize the dialog box settings
  221. SendDlgItemMessageW (hDlg, IDC_LOG_FILE_EDIT, EM_LIMITTEXT,
  222. (WPARAM)MAX_PATH-1, 0);
  223. if (pInfo->dwFlags & PDHI_DATA_SOURCE_CURRENT_ACTIVITY) {
  224. // check the correct radio button
  225. nButton = IDC_PERF_REG;
  226. nShowEdit = FALSE;
  227. nShowBrowse = FALSE;
  228. nShowRegBtn = TRUE;
  229. nShowWbemBtn = TRUE;
  230. hwndFocus = GetDlgItem(hDlg, IDC_PERF_REG);
  231. } else if (pInfo->dwFlags & PDHI_DATA_SOURCE_LOG_FILE) {
  232. // check the correct radio button
  233. nButton = IDC_DATA_FROM_LOG_FILE;
  234. nShowEdit = TRUE;
  235. nShowBrowse = TRUE;
  236. nShowRegBtn = FALSE;
  237. nShowWbemBtn = FALSE;
  238. // load log file to edit window
  239. SendDlgItemMessageW (hDlg, IDC_LOG_FILE_EDIT, WM_SETTEXT,
  240. (WPARAM)0, (LPARAM)pInfo->szDataSourceFile);
  241. hwndFocus = GetDlgItem(hDlg, IDC_LOG_FILE_EDIT);
  242. } else if (pInfo->dwFlags & PDHI_DATA_SOURCE_WBEM_NAMESPACE) {
  243. // check the correct radio button
  244. nButton = IDC_WBEM_NS;
  245. nShowEdit = FALSE;
  246. nShowBrowse = FALSE;
  247. nShowRegBtn = TRUE;
  248. nShowWbemBtn = TRUE;
  249. // if the file name has a "WBEM:" in the front, then remove it
  250. if (DataSourceTypeW(pInfo->szDataSourceFile) == DATA_SOURCE_WBEM) {
  251. if (wcsncmp(pInfo->szDataSourceFile,
  252. cszWBEM, lstrlenW(cszWBEM)) == 0) {
  253. szDisplayString = & pInfo->szDataSourceFile[lstrlenW(cszWBEM)];
  254. }
  255. else {
  256. szDisplayString = & pInfo->szDataSourceFile[lstrlenW(cszWMI)];
  257. }
  258. } else {
  259. szDisplayString = &pInfo->szDataSourceFile[0];
  260. }
  261. hwndFocus = GetDlgItem(hDlg, IDC_WBEM_NS);
  262. } else {
  263. // invalid selection
  264. SetLastError (PDH_INVALID_ARGUMENT);
  265. EndDialog (hDlg, IDCANCEL);
  266. goto INIT_EXIT;
  267. }
  268. if (nShowEdit) {
  269. // if this isn't selected, then set it so that it acts like the
  270. // default
  271. CheckRadioButton (hDlg, IDC_PERF_REG, IDC_WBEM_NS,
  272. IDC_PERF_REG);
  273. CheckRadioButton (hDlg, IDC_CURRENT_ACTIVITY, IDC_DATA_FROM_LOG_FILE,
  274. IDC_DATA_FROM_LOG_FILE);
  275. } else {
  276. CheckRadioButton (hDlg, IDC_CURRENT_ACTIVITY, IDC_DATA_FROM_LOG_FILE,
  277. IDC_CURRENT_ACTIVITY);
  278. CheckRadioButton (hDlg, IDC_PERF_REG, IDC_WBEM_NS,
  279. nButton);
  280. }
  281. // disable the edit window and browser button
  282. EnableWindow (GetDlgItem (hDlg, IDC_LOG_FILE_EDIT), nShowEdit);
  283. EnableWindow (GetDlgItem (hDlg, IDC_BROWSE_LOG_FILES), nShowBrowse);
  284. EnableWindow (GetDlgItem (hDlg, IDC_PERF_REG), nShowRegBtn);
  285. EnableWindow (GetDlgItem (hDlg, IDC_WBEM_NS), nShowWbemBtn);
  286. SetFocus (hwndFocus);
  287. bReturn = FALSE;
  288. INIT_EXIT:
  289. // restore Cursor
  290. SetCursor (hOldCursor);
  291. return bReturn;
  292. }
  293. STATIC_BOOL
  294. DataSrcDlg_WM_COMMAND (
  295. IN HWND hDlg,
  296. IN WPARAM wParam,
  297. IN LPARAM lParam
  298. )
  299. {
  300. WORD wNotifyMsg;
  301. wNotifyMsg = HIWORD(wParam);
  302. switch (LOWORD(wParam)) { // select on the control ID
  303. case IDOK:
  304. return DataSrcDlg_OK (hDlg, wNotifyMsg, (HWND)lParam);
  305. case IDCANCEL:
  306. return DataSrcDlg_CANCEL (hDlg, wNotifyMsg, (HWND)lParam);
  307. case IDC_HELP_BTN:
  308. return DataSrcDlg_HELP_BTN (hDlg, wNotifyMsg, (HWND)lParam);
  309. case IDC_BROWSE_LOG_FILES:
  310. return DataSrcDlg_BROWSE_LOG_FILES (hDlg, wNotifyMsg, (HWND)lParam);
  311. case IDC_CURRENT_ACTIVITY:
  312. case IDC_DATA_FROM_LOG_FILE:
  313. case IDC_PERF_REG:
  314. case IDC_WBEM_NS:
  315. return DataSrcDlg_RadioButton (hDlg, wNotifyMsg, LOWORD(wParam));
  316. default:
  317. return FALSE;
  318. }
  319. }
  320. STATIC_BOOL
  321. DataSrcDlg_WM_SYSCOMMAND (
  322. IN HWND hDlg,
  323. IN WPARAM wParam,
  324. IN LPARAM lParam
  325. )
  326. {
  327. UNREFERENCED_PARAMETER (lParam);
  328. switch (wParam) {
  329. case SC_CLOSE:
  330. EndDialog (hDlg, IDOK);
  331. return TRUE;
  332. default:
  333. return FALSE;
  334. }
  335. }
  336. STATIC_BOOL
  337. DataSrcDlg_WM_CLOSE (
  338. IN HWND hDlg,
  339. IN WPARAM wParam,
  340. IN LPARAM lParam
  341. )
  342. {
  343. UNREFERENCED_PARAMETER (hDlg);
  344. UNREFERENCED_PARAMETER (wParam);
  345. UNREFERENCED_PARAMETER (lParam);
  346. return TRUE;
  347. }
  348. STATIC_BOOL
  349. DataSrcDlg_WM_DESTROY (
  350. IN HWND hDlg,
  351. IN WPARAM wParam,
  352. IN LPARAM lParam
  353. )
  354. {
  355. UNREFERENCED_PARAMETER (hDlg);
  356. UNREFERENCED_PARAMETER (wParam);
  357. UNREFERENCED_PARAMETER (lParam);
  358. return TRUE;
  359. }
  360. INT_PTR
  361. CALLBACK
  362. DataSrcDlgProc (
  363. IN HWND hDlg,
  364. IN UINT message,
  365. IN WPARAM wParam,
  366. IN LPARAM lParam
  367. )
  368. {
  369. INT iCtrlID;
  370. BOOL bReturn = FALSE;
  371. iCtrlID = GetDlgCtrlID ( (HWND) wParam );
  372. switch (message) {
  373. case WM_INITDIALOG:
  374. return DataSrcDlg_WM_INITDIALOG (hDlg, wParam, lParam);
  375. case WM_COMMAND:
  376. return DataSrcDlg_WM_COMMAND (hDlg, wParam, lParam);
  377. case WM_SYSCOMMAND:
  378. return DataSrcDlg_WM_SYSCOMMAND (hDlg, wParam, lParam);
  379. case WM_CLOSE:
  380. return DataSrcDlg_WM_CLOSE (hDlg, wParam, lParam);
  381. case WM_DESTROY:
  382. return DataSrcDlg_WM_DESTROY (hDlg, wParam, lParam);
  383. case WM_CONTEXTMENU:
  384. {
  385. if ( 0 != iCtrlID ) {
  386. TCHAR pszHelpFilePath[MAX_PATH * 2];
  387. UINT nLen;
  388. nLen = GetWindowsDirectory(pszHelpFilePath, 2*MAX_PATH);
  389. lstrcpy(&pszHelpFilePath[nLen], _T("\\help\\sysmon.hlp") );
  390. bReturn = WinHelp(
  391. (HWND) wParam,
  392. pszHelpFilePath,
  393. HELP_CONTEXTMENU,
  394. (DWORD_PTR) PdhiDatasrcaulControlIdToHelpIdMap);
  395. }
  396. return bReturn;
  397. }
  398. case WM_HELP:
  399. {
  400. // Only display help for known context IDs.
  401. TCHAR pszHelpFilePath[MAX_PATH * 2];
  402. UINT nLen;
  403. LPHELPINFO pInfo = NULL;
  404. pInfo = (LPHELPINFO)lParam;
  405. nLen = GetWindowsDirectory(pszHelpFilePath, 2*MAX_PATH);
  406. if ( nLen == 0 ) {
  407. // Report error.
  408. }
  409. lstrcpy(&pszHelpFilePath[nLen], _T("\\help\\sysmon.hlp") );
  410. if (pInfo->iContextType == HELPINFO_WINDOW){
  411. bReturn = WinHelp (
  412. pInfo->hItemHandle,
  413. pszHelpFilePath,
  414. HELP_WM_HELP,
  415. (DWORD_PTR) PdhiDatasrcaulControlIdToHelpIdMap);
  416. }
  417. return bReturn;
  418. }
  419. default:
  420. return FALSE;
  421. }
  422. }
  423. PDH_FUNCTION
  424. PdhSelectDataSourceW (
  425. IN HWND hWndOwner,
  426. IN DWORD dwFlags,
  427. IN LPWSTR szDataSource,
  428. IN LPDWORD pcchBufferLength
  429. )
  430. {
  431. PDHI_DATA_SOURCE_INFO dsInfo;
  432. WCHAR wTest;
  433. DWORD dwTest;
  434. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  435. int nDlgBoxStatus;
  436. LPWSTR szLocalPath;
  437. DWORD dwLocalLength = 0;
  438. // TODO post W2k1: PdhiBrowseDataSource should be in try_except
  439. if ((szDataSource == NULL) || (pcchBufferLength == NULL))
  440. pdhStatus = PDH_INVALID_ARGUMENT;
  441. else {
  442. // test buffers and access
  443. __try {
  444. // test reading length buffer
  445. dwLocalLength = *pcchBufferLength;
  446. dwTest = dwLocalLength;
  447. // try reading & writing to the first and last chars in the buffer
  448. wTest = szDataSource[0];
  449. szDataSource[0] = 0;
  450. szDataSource[0] = wTest;
  451. dwTest--;
  452. wTest = szDataSource[dwTest];
  453. szDataSource[dwTest] = 0;
  454. szDataSource[dwTest] = wTest;
  455. } __except (EXCEPTION_EXECUTE_HANDLER) {
  456. pdhStatus = PDH_INVALID_ARGUMENT;
  457. }
  458. }
  459. if (pdhStatus == ERROR_SUCCESS) {
  460. szLocalPath = G_ALLOC ((dwLocalLength * sizeof(WCHAR)));
  461. if (szLocalPath == NULL) {
  462. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  463. } else {
  464. // copy the caller's buffer to the local buffer
  465. memcpy (&szLocalPath[0], szDataSource,
  466. (dwLocalLength * sizeof(WCHAR)));
  467. }
  468. if (pdhStatus == ERROR_SUCCESS) {
  469. if (dwFlags & PDH_FLAGS_FILE_BROWSER_ONLY) {
  470. PdhiBrowseDataSource (
  471. hWndOwner,
  472. (LPVOID)szDataSource,
  473. &dwLocalLength,
  474. TRUE);
  475. } else {
  476. // show the selection dialog as well
  477. if (*szDataSource == 0) {
  478. // then using current activity
  479. dsInfo.dwFlags = PDHI_DATA_SOURCE_CURRENT_ACTIVITY;
  480. } else {
  481. if (IsWbemDataSource (szDataSource)) {
  482. dsInfo.dwFlags = PDHI_DATA_SOURCE_WBEM_NAMESPACE;
  483. } else {
  484. dsInfo.dwFlags = PDHI_DATA_SOURCE_LOG_FILE;
  485. }
  486. }
  487. dsInfo.szDataSourceFile = szLocalPath;
  488. dsInfo.cchBufferLength = dwLocalLength;
  489. // call dialog box
  490. nDlgBoxStatus = (INT)DialogBoxParamW (
  491. (HINSTANCE)ThisDLLHandle,
  492. MAKEINTRESOURCEW (IDD_DATA_SOURCE),
  493. hWndOwner,
  494. DataSrcDlgProc,
  495. (LPARAM)&dsInfo);
  496. if (nDlgBoxStatus == IDOK) {
  497. pdhStatus = ERROR_SUCCESS;
  498. dwLocalLength = dsInfo.cchBufferLength;
  499. __try {
  500. memcpy (szDataSource, &szLocalPath[0],
  501. (dwLocalLength * sizeof(WCHAR)));
  502. } __except (EXCEPTION_EXECUTE_HANDLER) {
  503. pdhStatus = PDH_INVALID_ARGUMENT;
  504. }
  505. } // else, leave the caller's buffer alone
  506. }
  507. if (pdhStatus == ERROR_SUCCESS) {
  508. __try {
  509. *pcchBufferLength = dwLocalLength;
  510. } __except (EXCEPTION_EXECUTE_HANDLER) {
  511. pdhStatus = PDH_INVALID_ARGUMENT;
  512. }
  513. }
  514. }
  515. if (szLocalPath != NULL) {
  516. G_FREE (szLocalPath);
  517. }
  518. }
  519. return pdhStatus;
  520. }
  521. PDH_FUNCTION
  522. PdhSelectDataSourceA (
  523. IN HWND hWndOwner,
  524. IN DWORD dwFlags,
  525. IN LPSTR szDataSource,
  526. IN LPDWORD pcchBufferLength
  527. )
  528. {
  529. CHAR cTest;
  530. DWORD dwTest;
  531. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  532. LPWSTR szWideBuffer;
  533. DWORD dwLocalLength = 0;
  534. // TODO post W2k1: PdhiBrowseDataSource should be in try_except
  535. if ((szDataSource == NULL) || (pcchBufferLength == NULL))
  536. pdhStatus = PDH_INVALID_ARGUMENT;
  537. else {
  538. // test buffers and access
  539. __try {
  540. // test reading length buffer
  541. dwLocalLength = *pcchBufferLength;
  542. dwTest = dwLocalLength;
  543. // try reading & writing to the first and last chars in the buffer
  544. cTest = szDataSource[0];
  545. szDataSource[0] = 0;
  546. szDataSource[0] = cTest;
  547. dwTest--;
  548. cTest = szDataSource[dwTest];
  549. szDataSource[dwTest] = 0;
  550. szDataSource[dwTest] = cTest;
  551. } __except (EXCEPTION_EXECUTE_HANDLER) {
  552. pdhStatus = PDH_INVALID_ARGUMENT;
  553. }
  554. }
  555. if (pdhStatus == ERROR_SUCCESS) {
  556. if (dwFlags & PDH_FLAGS_FILE_BROWSER_ONLY) {
  557. PdhiBrowseDataSource (
  558. hWndOwner,
  559. (LPVOID)szDataSource,
  560. & dwLocalLength,
  561. FALSE);
  562. } else {
  563. // allocate a temporary bufer and convert the ANSI string to a wide
  564. szWideBuffer = G_ALLOC ((dwLocalLength * sizeof(WCHAR)));
  565. if (szWideBuffer != NULL) {
  566. MultiByteToWideChar(_getmbcp(),
  567. 0,
  568. szDataSource,
  569. lstrlenA(szDataSource),
  570. (LPWSTR) szWideBuffer,
  571. dwLocalLength);
  572. pdhStatus = PdhSelectDataSourceW (
  573. hWndOwner, dwFlags, szWideBuffer, &dwLocalLength);
  574. if (pdhStatus == ERROR_SUCCESS) {
  575. // if a null string was returned, then set the argument
  576. // to null since the conversion routine will not convert
  577. // a null wide string to a null ansi string.
  578. if (*szWideBuffer == 0) {
  579. *szDataSource = 0;
  580. } else {
  581. pdhStatus = PdhiConvertUnicodeToAnsi(_getmbcp(),
  582. szWideBuffer, szDataSource, & dwLocalLength);
  583. }
  584. }
  585. G_FREE (szWideBuffer);
  586. } else {
  587. // unable to allocate temporary buffer
  588. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  589. }
  590. }
  591. if (pdhStatus == ERROR_SUCCESS || pdhStatus == PDH_MORE_DATA) {
  592. __try {
  593. *pcchBufferLength = dwLocalLength;
  594. } __except (EXCEPTION_EXECUTE_HANDLER) {
  595. pdhStatus = PDH_INVALID_ARGUMENT;
  596. }
  597. }
  598. }
  599. return pdhStatus;
  600. }