Introduction
This article presents a no-frills, cut-n-paste solution to the age-old problem of browsing for a folder. There are several classes out here that offer much more customization and a plethora of features, but I'm sure one more example won't hurt.
When to use this solution
The benefit of this approach over some of the others here is that this implementation is a direct wrapper function for SHBrowseForFolder
. As such, you can use it in console applications that don't use MFC, such as unit-testing applications that could benefit from not hard-coding an output location for test results.
So, in short, if you're looking for eye candy, check out XFolderDialog or some of the other folder dialogs here. Otherwise, read on.
Using the code
To use this code, you need to include the following standard files:
#include "shlobj.h"
#include <string>
The GetFolder function
Params
folderpath
- A reference to the string that will contain the folder path if successful.
szCaption
- An optional message for the dialog.
hOwner
- An optional handle to the parent window.
Returns
true
if successful, false
if cancelled or an error occurs.
bool GetFolder(std::string& folderpath,
const char* szCaption = NULL,
HWND hOwner = NULL)
{
bool retVal = false;
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_USENEWUI;
bi.hwndOwner = hOwner;
bi.lpszTitle = szCaption;
::OleInitialize(NULL);
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
if(pIDL != NULL)
{
char buffer[_MAX_PATH] = {'\0'};
if(::SHGetPathFromIDList(pIDL, buffer) != 0)
{
folderpath = buffer;
retVal = true;
}
CoTaskMemFree(pIDL);
}
::OleUninitialize();
return retVal;
}
Conclusion
Well, that's really all there is to it. If this code saves time for even one person out here, then the few minutes spent writing this was well worth it. Enjoy!