Aug 142008

DlgDirList, DlgDirSelect, DlgDirListComboBox, CB_DIR and LB_DIR are some functions and event ids that helps us in making our normal list and combo boxes more powerful by enabling directory and file listing capabilities with just one function call or one event.

Take a look at this screenshot before proceeding, the exe is here(save as exe), easy to implement hence full source not shared.

DlgDirList and DlgDirListComboBox Demo

DlgDirList and DlgDirListComboBox Demo

So here is a list of these API’s. I’ll be taking them one by one, have fun!

  1. DlgDirList
  2. DlgDirListComboBox
  3. LB_DIR (Equivalent to DlgDirList)
  4. CB_DIR (Equivalent to DlgDirListComboBox)
  5. DlgDirSelect
  6. DlgDirSelectEx (More powerful than DlgDirSelect)
  7. DlgDirSelectComboBox
  8. DlgDirSelectComboBoxEx (More power than DlgDirSelectComboBoxEx)


Here is an example which dumps all the files and folders in C: drive to a listbox.

DlgDirList( _T( "C:" ), 

If you notice the third parameter of DlgDirList looks quite strange isn’t it? It takes an edit control id but actually MSDN tells that we should give a static control id, I was testing to see whether it will work with edit controls too. So why is this parameter given? After you call this function, the given edit/static control will have their text set as “C:”. 😉


And here is another one which dumps all the files in C: drive to a combo box.

DlgDirListComboBox( _T( "C:" ),
                    DDL_READWRITE | DDL_DIRECTORY );

Explanation for third parameter is the same as for DlgDirList.


Similar to DlgDirList. Here is an e.g. on how to fire this event.

SendDlgItemMessage( IDC_LIST_FILE_LIST,
                    reinterpret_cast<lparam>( "C:*.*" ));

Note that we’ve got to specify wild card characters to make this event succeed. DlgDirList and its peers do this by themselves so we needn’t do it. So if we specify *.txt instead of *.* only directories and text files will be listed. 🙂

This also unveals the main power of these API’s. We can write quite useful file system search tools using these API’s. I’ve uploaded a demo, do take a look!


Similar to LB_DIR, just change the id to combo control id and the event name to CB_DIR.


So what does DlgDirSelect do? Retrieves the current selection from a list box. It assumes that the list box has been filled by the DlgDirList member function and that the selection is a drive letter, a file, or a directory name.

An e.g. :  Lets suppose there is a directory called [Documents and settings] in our list and it’s selected, then DlgDirSelect returns “Documents and Settings”.

DlgDirSelect( szPath, IDC_LIST_FILE_LIST );

Note that DlgDirSelect does not do much of an error checking.


Note that if you using functions DlgDirList, DlgDirListComboBox then directory names will be wrapped in square brackets, for e.g. [Program Files].  DlgDirSelectEx removes these square brackets.

Some rules for DlgDirSelectEx

  1. Must be a single selection list box, if not the buffer given does not change.
  2. If selection is a directory then return value is non zero, else zero.
  3. Removes square brackets from directory names making them ready for insertion to any path.

An e.g. : Lets suppose there is a directory called [Documents and settings] in our list and it’s selected, then DlgDirSelectEx returns “Documents and Settings”.

DlgDirSelectEx( GetSafeHwnd(),
                IDC_LIST_FILE_LIST );


Similar to DlgDirSelect


Similar to DlgDirSelectEx. Rules also are same.

  3 Responses to “Using DlgDirList, DlgDirListComboBox, CB_DIR, LB_DIR, DlgDirSelect, DlgDirSelectEx, DlgDirSelectComboBox, DlgDirSelectComboBoxEx set of functions”

  1. You are welcome James.

  2. Thanks for this. I’ve been struggling badly. Now I’ve
    got something smarter to try.
    My Respects!

  3. Thanks for this. I’ve been struggling badly. Now I’ve got something smarter to try.
    My Respects!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.