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.

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