GetFolder

BlackDice 0 Tallied Votes 193 Views Share

Returns the name of a folder in a CString after allowing user to browse for it. This is just a wrapper around the SHBrowseForFolder API, but it's great for only allowing a user to browse for folders!

CString GetFolder()
{
		
		CString strDirectory;	
		
			LPMALLOC pMalloc;
			//set up BROWSEINFO struct for the ShBrowseForFolder dialog box
			BROWSEINFO bi;
			CString strPath1,strPath2;
			//Gets the Shell's default allocator (260)
			char pszBuffer[MAX_PATH];
			LPITEMIDLIST pidl;
			// Get help on BROWSEINFO struct - it's got all the bit settings.
			bi.hwndOwner = GetDesktopWindow();
			bi.pidlRoot = NULL;
			bi.pszDisplayName = pszBuffer;
			bi.lpszTitle = _T("YOUR TITLE");
			bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
			bi.lpfn = NULL;
			bi.lParam = 0;

			
			if (::SHGetMalloc(&pMalloc) == NOERROR)
			{
				if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
				{
					if (::SHGetPathFromIDList(pidl, pszBuffer))
					{ 
						strDirectory = pszBuffer;
					}
					pMalloc->Free(pidl);
				}
				pMalloc->Release();
			}
			

        return strDirectory;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.