HOWTO: Browse for Folders from the Current Directory

Last reviewed: March 10, 1998
Article ID: Q179378
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

    - Microsoft Windows NT 4.0

    - Microsoft Windows 95

SUMMARY

By default, the SHBrowseForFolder API lets the user start at the desktop to browse the shell's namespace and pick a folder. Often, you may prefer that your application start the browse dialog box at a folder that the user is likely to want, such as the current working directory.

To set the browse dialog box's initial selection, the BROWSEINFO structure must contain a callback function. When the callback function is called with the message BFFM_INITIALIZED, it can in turn send a BFFM_SETSELECTION message to set the dialog box's selection to the desired path.

MORE INFORMATION

Following is some sample code that brings up the browse dialog box with the current directory selected. It also displays the path of the currently selected folder in the dialog box's status window.

Sample Code

      #define STRICT
      #include <windows.h>
      #include <shlobj.h>

      int CALLBACK
      BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) {
         TCHAR szDir[MAX_PATH];

         switch(uMsg) {
            case BFFM_INITIALIZED: {
               if GetCurrentDirectory(sizeof(szDir)/sizeof(TCHAR),
                                      szDir)) {
                  // WParam is TRUE since you are passing a path.
                  // It would be FALSE if you were passing a pidl.
                  SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szDir);
               }
               break;
            }
            case BFFM_SELCHANGED: {
               // Set the status window to the currently selected path.
               if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir)) {
                  SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
               }
               break;
            }
            default:
               break;
         }
         return 0;
      }

      int PASCAL
      WinMain (HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpszCmdLine,
               int nCmdShow)
      {
         BROWSEINFO bi;
         TCHAR szDir[MAX_PATH];
         LPITEMIDLIST pidl;
         LPMALLOC pMalloc;

         if (SUCCEEDED(SHGetMalloc(&pMalloc))) {
            ZeroMemory(&bi,sizeof(bi));
            bi.hwndOwner = NULL;
            bi.pszDisplayName = 0;
            bi.pidlRoot = 0;
            bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
            bi.lpfn = BrowseCallbackProc;

            pidl = SHBrowseForFolder(&bi);
            if (pidl) {
               if (SHGetPathFromIDList(pidl,szDir)) {
                  MessageBox(NULL,szDir,"Picked",MB_OK);
               }

               // In C++: pMalloc->Free(pidl); pMalloc->Release();
               pMalloc->lpVtbl->Free(pMalloc,pidl);
               pMalloc->lpVtbl->Release(pMalloc);
            }
         }
         return 0;
      }
Keywords          : UsrShell kbcode
Version           : WINNT:4.0
Platform          : winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 10, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.