Click here to Skip to main content
12,820,546 members (29,312 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 16 Apr 2009

CBrowseFolderDialog Class

, 20 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple BrowseForFolder dialog functionality implementation.



Almost every time I develop an application, I need the «Browse For Folder» dialog functionality. But every time I use it, I have to allocate and initialize the BROWSEINFO structure and call SHGetMalloc(), SHGetFolderLocation(), SHGetPathFromIDList(), etc.

Obviously, this code would have to be duplicated a lot of times. To avoid such duplications, I created a class that encapsulates all the above mentioned code inside.

Using the Code

What you have to do if you want to put it to work:

  1. Download the source code and add it to your project.
  2. Create a CBrowseFolderDialog instance.
  3. Call the CBrowseFolderDialog::BrowseFolder() member function that creates a dialog and returns the path selected. Here is how the function is declared:
PCTSTR BrowseFolder(HWND hwndOwner = NULL, // parent window handle
        PCTSTR pTitle = NULL, // text that will be shown in the top of dialog
        int nCSIDL = 0,   // here you can specify initial folder CSIDL
        LPARAM lParam = NULL, // here you can specify initial folder path
        UINT uFlags = BIF_NEWDIALOGSTYLE,  // dialog flags
        BFFCALLBACK callbackProc = NULL // callback procedure,
                // use NULL if you will not provide it);

As you can see, all the parameters have default values. Here are several examples of using CBrowseFolderDialog in the code:

CBrowseFolderDialog dlg;

// example 1:
CString strFolderPath = dlg.BrowseFolder();

// example 2:
strFolderPath = dlg.BrowseFolder(NULL, TEXT("SELECT FOLDER!"));

// example 3: specifying initial folder CSIDL
strFolderPath = dlg.BrowseFolder(CSIDL_DESKTOP);

// example 4: specifying initial folder Path
strFolder = dlg.BrowseFolder(NULL, NULL, NULL, (LPARAM)_T("C:\\"));

// example 5: specifying your own browse
// callback procedure as 6-th parameter
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, 
                    LPARAM lParam, LPARAM lpData)
    // some implementation
    return 0;

strFolder = dlg.BrowseFolder(NULL, NULL, NULL, NULL, 
                             NULL, BrowseCallbackProc);

I have to explain a little about the fifth parameter. It is the BROWSEINFO flags, and is explained in detail on MSDN. The default value is BIF_NEWDIALOGSTYLE. If you want to get a different behaviour, specify your own flag combination here.


  • 16th April, 2009: Initial post.
  • 17th April, 2009: Article updated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Denis Ponomarenko
Software Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhat is new about your code Pin
rajas16-Apr-09 16:50
memberrajas16-Apr-09 16:50 
AnswerRe: What is new about your code Pin
Denis Ponomarenko16-Apr-09 23:04
memberDenis Ponomarenko16-Apr-09 23:04 
GeneralNice. Pin
c-smile16-Apr-09 9:59
memberc-smile16-Apr-09 9:59 
AnswerRe: Nice. Pin
Denis Ponomarenko16-Apr-09 23:10
memberDenis Ponomarenko16-Apr-09 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 20 Apr 2009
Article Copyright 2009 by Denis Ponomarenko
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid