By default, the SHBrowseForFolder API lets the user start at the desktop to
browse the shell's namespace and pick a folder. Often, you may prefer that
your application start the browse dialog box at a folder that the user is
likely to want, such as the current working directory.
To set the browse dialog box's initial selection, the BROWSEINFO structure
must contain a callback function. When the callback function is called with
the message BFFM_INITIALIZED, it can in turn send a BFFM_SETSELECTION
message to set the dialog box's selection to the desired path.
Following is some sample code that brings up the browse dialog box with the
current directory selected. It also displays the path of the currently
selected folder in the dialog box's status window.
INT CALLBACK BrowseCallbackProc(HWND hwnd,
if (GetCurrentDirectory(sizeof(szDir)/sizeof(TCHAR), szDir))
// WParam is TRUE since you are passing a path.
// It would be FALSE if you were passing a pidl.
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
// Set the status window to the currently selected path.
if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir))
int APIENTRY WinMain(HINSTANCE hInstance,
bi.hwndOwner = NULL;
bi.pszDisplayName = 0;
bi.pidlRoot = 0;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = BrowseCallbackProc;
pidl = SHBrowseForFolder(&bi);
// Other code omited
Article ID: 179378 - Last Review: November 21, 2006 - Revision: 1.3
- Microsoft Win32 Application Programming Interface
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.