1

I have a windows application written in C++. The application generates certain configuration files in a hidden directory. I want to give user an option to open that directory from my application. Clicking that option should open a windows explorer like dialog with an input directory location. I spend time searching for a similar api, but end up with certain dialogs like "DlgDirListComboBoxW" or "GetOpenFileName" or "GetSaveFileName". I am looking for an api to open normal Windows explorer like Dialog with an input directory location. It would be really helpful if the api belongs to CommonDialogs section.

aksjain
  • 19
  • 3

2 Answers2

1

How about:

HWND hWndOwner = NULL;

ShellExecute(
    hWndOwner,
    _T("explore"),
    _T("c:\\some\\path"),
    NULL,
    NULL,
    SW_SHOWNORMAL);

You can set hWndOwner to your main window handle if you're so inclined and can choose from a variety of other options.

For more information and usage details, check out the MSDN page on ShellExecute.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • Fixed the `explore` typo for you. I don't think it would be right to also fix the `_T` stuff, since it's your code. But it's a pretty bad thing to do to teach that to anybody, and especially to newbies. – Cheers and hth. - Alf Jan 21 '15 at 07:20
  • @Cheersandhth.-Alf It was not a typo. The correct keyword is `explore` (per the MSDN page). As for the `_T` bits: I don't know if his program is MBCS or UNICODE and since `ShellExecute` comes in A and W varieties, I have to use the "portable" way. If you know of a better way of passing a literal that may be either MBCS or UNICODE depending on project configuration, by all means, let me know. – Nik Bougalis Jan 21 '15 at 09:03
  • 1
    A better way than using `_T` (paying a heavy price to support WIndows 9x), is to not use `_T`. You can always assumed UNICODE except for legacy code from the early 1990s. `_T` is in support of Windows 9x, and it was obsolete already in the year 2000 with the introduction of Layer for Unicode, i.e. a now 15 years obsolete dirty macro-based technology in support of a 20 year old anachronism. – Cheers and hth. - Alf Jan 21 '15 at 10:26
  • Sorry about mistaking the verb for an executable name. – Cheers and hth. - Alf Jan 21 '15 at 10:27
1

You can use the SHBrowseForFolder

It shows a dialog similar to this:

enter image description here

This is a example for how to use it:

BOOL GetFolder(LPCSTR folderpath, 
               LPCSTR szCaption, 
               HWND hOwner /*= NULL*/)
{
    BOOL retVal = FALSE;

    // The BROWSEINFO struct tells the shell 
    // how it should display the dialog.
    BROWSEINFO bi;
    memset(&bi, 0, sizeof(bi));

    bi.ulFlags   = BIF_USENEWUI;
    bi.hwndOwner = hOwner;
    bi.lpszTitle = szCaption;   

    // must call this if using BIF_USENEWUI
    ::OleInitialize(NULL);

    // Show the dialog and get the itemIDList for the 
    // selected folder.
    LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

    if(pIDL != NULL)
    {
        // Create a buffer to store the path, then 
        // get the path.
        char buffer[_MAX_PATH] = {'\0'};
        if(::SHGetPathFromIDList(pIDL, buffer) != 0)
        {
            // Set the string value.
            folderpath = buffer;
            retVal = TRUE;
        }

        // free the item id list
        CoTaskMemFree(pIDL);
    }

    ::OleUninitialize();

    return retVal;
}
Lucian
  • 3,407
  • 2
  • 21
  • 18