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.

104 lines
2.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: browsedi.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // BrowseDir.cpp
  12. //
  13. #include "pch.cpp"
  14. #pragma hdrstop
  15. #include <shlobj.h>
  16. #define __dwFILE__ __dwFILE_OCMSETUP_BROWSEDI_CPP__
  17. int
  18. InitStartDir(
  19. HWND hwnd,
  20. UINT uMsg,
  21. LPARAM lParam,
  22. LPARAM lpData)
  23. {
  24. // we just capture Init Message
  25. if (BFFM_INITIALIZED == uMsg)
  26. {
  27. // we expect lpData to be our start path
  28. SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, lpData);
  29. }
  30. return 0;
  31. }
  32. BOOL BrowseForDirectory(
  33. HWND hwndParent,
  34. LPCTSTR pszInitialDir,
  35. LPTSTR pszBuf,
  36. int cchBuf,
  37. LPCTSTR pszDialogTitle,
  38. BOOL bRemoveTrailingBackslash )
  39. {
  40. LPITEMIDLIST pItem = NULL;
  41. TCHAR szPath[MAX_PATH+1];
  42. BOOL bGotLocation = FALSE;
  43. BROWSEINFO bi;
  44. ZeroMemory(&bi, sizeof(bi));
  45. bi.hwndOwner = hwndParent;
  46. // initial folder
  47. if (pszInitialDir != NULL)
  48. {
  49. bi.lpfn = InitStartDir;
  50. bi.lParam = (LPARAM)pszInitialDir;
  51. }
  52. bi.pszDisplayName = szPath;
  53. bi.lpszTitle = pszDialogTitle;
  54. bi.ulFlags = BIF_USENEWUI |
  55. BIF_SHAREABLE |
  56. BIF_RETURNONLYFSDIRS; // return only directories in the filesystem, not other folders
  57. pItem = SHBrowseForFolder(&bi);
  58. if (pItem == NULL)
  59. goto Ret;
  60. bGotLocation = SHGetPathFromIDList(pItem, szPath);
  61. if (!bGotLocation)
  62. goto Ret;
  63. CSASSERT((_tcslen(szPath)+1)*sizeof(TCHAR) <= (ULONG)cchBuf);
  64. if ((_tcslen(szPath)+1)*sizeof(TCHAR) > (ULONG)cchBuf)
  65. return FALSE;
  66. _tcscpy(pszBuf, szPath);
  67. Ret:
  68. LPMALLOC pMalloc;
  69. if (SUCCEEDED(SHGetMalloc(&pMalloc)))
  70. {
  71. if (pItem)
  72. pMalloc->Free(pItem);
  73. if (bi.pidlRoot)
  74. pMalloc->Free((ITEMIDLIST*)bi.pidlRoot);
  75. pMalloc->Release();
  76. }
  77. return bGotLocation;
  78. }