void CSelectDrivesDlg::OnBnClickedBrowsefolder() { // Buffer, because SHBrowseForFolder() wants a buffer CString sDisplayName, sSelectedFolder = m_folderName; BROWSEINFO bi; ZeroMemory(&bi, sizeof(bi)); // Load a meaningful title for the browse dialog CString title= LoadString(IDS_SELECTFOLDER); bi.hwndOwner= m_hWnd; // Use the CString as buffer (minimum is MAX_PATH as length) bi.pszDisplayName= sDisplayName.GetBuffer(_MAX_PATH); bi.lpszTitle= title; // Set a callback function to pre-select a folder bi.lpfn = BFFCALLBACK(BrowseCallbackProc); bi.lParam = LPARAM(sSelectedFolder.GetBuffer()); // Set the required flags bi.ulFlags= BIF_RETURNONLYFSDIRS | BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_NONEWFOLDERBUTTON; LPITEMIDLIST pidl= SHBrowseForFolder(&bi); // Release the actual buffer sDisplayName.ReleaseBuffer(); sSelectedFolder.ReleaseBuffer(); if (pidl != NULL) { CString sDir; LPSHELLFOLDER pshf; HRESULT hr= SHGetDesktopFolder(&pshf); ASSERT(SUCCEEDED(hr)); STRRET strret; strret.uType= STRRET_CSTR; hr= pshf->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &strret); ASSERT(SUCCEEDED(hr)); sDir= MyStrRetToString(pidl, &strret); CoTaskMemFree(pidl); pshf->Release(); m_folderName= sDir; m_radio= RADIO_AFOLDER; UpdateData(false); UpdateButtons(); } }
// Retrieve the "fully qualified parse name" of "My Computer" CString GetParseNameOfMyComputer() { CComPtr<IShellFolder> sf; HRESULT hr = ::SHGetDesktopFolder(&sf); MdThrowFailed(hr, _T("::SHGetDesktopFolder")); CCoTaskMem<LPITEMIDLIST> pidl; hr = ::SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidl); MdThrowFailed(hr, _T("SHGetSpecialFolderLocation(CSIDL_DRIVES)")); STRRET name; ZeroMemory(&name, sizeof(name)); name.uType = STRRET_CSTR; hr = sf->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &name); MdThrowFailed(hr, _T("GetDisplayNameOf(My Computer)")); return MyStrRetToString(pidl, &name); }