11.24. How do I display a choose directory dialog, instead of a choose file dialog?

/* Work's only if we're 95 capable */
if (afxData.bWin4)
{
    LPMALLOC pMalloc;
   
    /* Get's 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 Starting Directory");
        bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
        bi.lpfn = NULL; bi.lParam = 0;
       
        // This next call issues the dialog box
        if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
        {
            if (::SHGetPathFromIDList(pidl, pszBuffer))
            {
                //At this point pszBuffer contains the selected path 
                DoingSomethingUseful(pszBuffer);
            }
           
            // Free the PIDL allocated by SHBrowseForFolder
            pMalloc->Free(pidl);
        }
       
        // Release the shell's allocator
        pMalloc->Release();
    }
}

NOTE: This code will work on Win95 only - it's part of the shell.

bradw@netnet.net mfc-l, 9/9/95