
| Last Modification: August 12, 2000 |
How is SHBrowseForFolder() used?
Several people have trouble getting to use SHBrowseForFolder(). It is, however,
very easy. Here's a short example:
BROWSEINFO bi = { 0 };
bi.lpszTitle = _T("Pick a Directory");
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder
TCHAR path[MAX_PATH];
if ( SHGetPathFromIDList ( pidl, path ) )
{
_tprintf ( _T("Selected Folder: %s\n"), path );
}
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
There is, however, one gotcha: SHGetPathFromIDList will fail if the selected item is
not part of the filesystem, i.e. when a computer or shell namespace is selected. In these cases,
you can do the following to get the object's name:
BROWSEINFO bi = { 0 };
TCHAR path[MAX_PATH];
bi.lpszTitle = _T("Pick a Directory");
bi.pszDisplayName = path;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder
_tprintf ( _T("Selected Item: %s\n"), path );
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
Here are a couple more tricks:
SHBrowseForFolder() to display only computer names, you can invoke it
with the BIF_BROWSEFORCOMPUTER flag. To improve things further, you can make the
dialog display only items in the network neighborhood, by getting a PIDL to it using
SHGetSpecialFolderLocation() first, and making it the root item in the dialog by
setting the BROWSEINFO::pidlRoot member.
BROWSEINFO struct as above. Take a look at
http://www.mvps.org/windev/articles/ishfolder.html