How to open a File Browse Dialog

How can I create a File browsing dialog that returns the directory chosen?

I tried this:

http://www.codeproject.com/dialog/cfolderdialog.asp?df=100&forumid=3533&select=957565

but can't get it to work (errors are posted at the bottom of that page)

thanks!
galneweinhawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Usually, you'd use 'SHBrowseForFolder()' to do that, e.g.

#include <shlobj.h>

void PickFolder ( CString& strFolder)
{

    LPMALLOC    pMalloc;    /* Gets the Shell's default allocator */

    if  (   ::SHGetMalloc   (   &pMalloc)   ==  NOERROR)
        {
            BROWSEINFO      bi;
            char            pszBuffer   [   MAX_PATH];
            LPITEMIDLIST    pidl;

            // Get help on BROWSEINFO struct - it's got all the bit settings.
            bi.hwndOwner        =   GetSafeHwnd();
            bi.pidlRoot         =   NULL;
            bi.pszDisplayName   =   pszBuffer;
            bi.lpszTitle        =   _T("Select a Folder");
            bi.ulFlags          =   0;
            bi.lpfn             =   NULL;
            bi.lParam           =   0;

            // This next call issues the dialog box.
            if  (   pidl    =   ::SHBrowseForFolder (   &bi))
                {
                    if  (   ::SHGetPathFromIDList   (   pidl, pszBuffer))
                        {
                            // At this point pszBuffer contains the selected folder */
                                          strFolder = pszBuffer;
                         }

                    // Free the PIDL allocated by SHBrowseForFolder.
                    pMalloc->Free   (   pidl);
                }

            // Release the shell's allocator.        
            pMalloc->Release();    
        }    
}
0
galneweinhawAuthor Commented:
That works great!

is there any way for me to specify the folder the browse window starts in? (without restricting the browsing by using a root)

thanks
0
jkrCommented:
http://www.codeproject.com/dialog/folderbrowser.asp ("How to show a dialog to let users browse for and select a folder") describes that issu epretty well, and better than I could do.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Nass89Commented:
Hi,
Have a look at "Q179378" "HOWTO: Browse for Folders from the Current Directory"
in MSDN.

Good Luck!
0
galneweinhawAuthor Commented:
I tried the codeproject thing but it's giving me this error...

Compiling...
UpdateIESDlg.cpp
O:\ENGINEERING\SHARED\PHOTOMET\PROGRAMS\OTHER PROGRAMS\UPDATEIES\UpdateIES\UpdateIESDlg.cpp(302) : error C2440: '=' : cannot convert from 'int (__stdcall CUpdateIESDlg::*)(struct HWND__ *,unsigned int,long,long)' to 'int (__stdcall *)(struct HWND__
*,unsigned int,long,long)'
        There is no context in which this conversion is possible
O:\ENGINEERING\SHARED\PHOTOMET\PROGRAMS\OTHER PROGRAMS\UPDATEIES\UpdateIES\UpdateIESDlg.cpp(312) : warning C4390: ';' : empty controlled statement found; is this the intent?
Error executing cl.exe.
Creating browse info file...

UpdateIES.exe - 1 error(s), 1 warning(s)


The first occurs here:
bi.lpfn = BrowseCallbackProc;

The warning here:
        if(SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc);
        pMalloc->Free(pidl);  
        pMalloc->Release();


thanks for the help.
0
jkrCommented:
Nass89 is right http://support.microsoft.com/default.aspx?scid=kb;en-us;179378 ("How To Browse for Folders from the Current Directory") has an easier example. With the above code, that should be

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;
   }
   return 0;
}

void PickFolder ( CString& strFolder)
{

   LPMALLOC    pMalloc;    /* Gets the Shell's default allocator */

   if  (   ::SHGetMalloc   (   &pMalloc)   ==  NOERROR)
       {
           BROWSEINFO      bi;
           char            pszBuffer   [   MAX_PATH];
           LPITEMIDLIST    pidl;

           // Get help on BROWSEINFO struct - it's got all the bit settings.
           bi.hwndOwner        =   GetSafeHwnd();
           bi.pidlRoot         =   NULL;
           bi.pszDisplayName   =   pszBuffer;
           bi.lpszTitle        =   _T("Select a Folder");
           bi.ulFlags          =   0;
           bi.lpfn             =   BrowseCallbackProc;
           bi.lParam           =   0;

           // This next call issues the dialog box.
           if  (   pidl    =   ::SHBrowseForFolder (   &bi))
               {
                   if  (   ::SHGetPathFromIDList   (   pidl, pszBuffer))
                       {
                           // At this point pszBuffer contains the selected folder */
                                  strFolder = pszBuffer;
                        }

                   // Free the PIDL allocated by SHBrowseForFolder.
                   pMalloc->Free   (   pidl);
               }

           // Release the shell's allocator.        
           pMalloc->Release();    
       }    
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
galneweinhawAuthor Commented:
Thanks to both of you for the help

unfortunately I am still getting this error using the code above:


error C2440: '=' : cannot convert from 'int (__stdcall CUpdateIESDlg::*)(struct HWND__ *,unsigned int,long,long)' to 'int (__stdcall *)(struct HWND__ *,unsigned int,long,long)' There is no context in which this conversion is possible


It is occuring at this line:
bi.lpfn             =   BrowseCallbackProc;



Also getting the error:
error C2660: 'SendMessageA' : function does not take 4 parameters

from lines:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
//and
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);

thanks again.
0
jkrCommented:
>>error C2440: '=' : cannot convert from

The browse callback cannot be a non-static member of CUpdateIESDlg - either make that not a member function or declare it as 'static'.

>>error C2660: 'SendMessageA' : function does not take 4 parameters

The compiler thinks you are referring to CWnd::SendMessage() - use

::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
//and
::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);

to resolve this ambiguity.
0
galneweinhawAuthor Commented:
Thanks jkr,

working great.

How do I post a follow-up question?

My directory name is so long it is wrapping and getting hidden behind the tree-view.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.