The following example uses the SHBrowseForFolder function to prompt the user for a program group. The Programs directory is specified as the root.
// Main_OnBrowse - browses for a program folder.
// hwnd - handle of the application's main window
//
// Uses the global variable g_pMalloc, which is assumed to point
// to the shell's IMalloc interface.
void Main_OnBrowse(HWND hwnd)
{
BROWSEINFO bi;
LPSTR lpBuffer;
LPITEMIDLIST pidlPrograms; // PIDL for Programs folder
LPITEMIDLIST pidlBrowse; // PIDL selected by user
// Allocate a buffer to receive browse information.
if ((lpBuffer = (LPSTR) g_pMalloc->lpVtbl->Alloc(
g_pMalloc, MAX_PATH)) == NULL)
return;
// Get the PIDL for the Programs folder.
if (!SUCCEEDED(SHGetSpecialFolderLocation(
hwnd, CSIDL_PROGRAMS, &pidlPrograms))) {
g_pMalloc->lpVtbl->Free(g_pMalloc, lpBuffer);
return;
}
// Fill in the BROWSEINFO structure.
bi.hwndOwner = hwnd;
bi.pidlRoot = pidlPrograms;
bi.pszDisplayName = lpBuffer;
bi.lpszTitle = "Choose a Program Group";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
// Browse for a folder and return its PIDL.
pidlBrowse = SHBrowseForFolder(&bi);
if (pidlBrowse != NULL) {
// Show the display name, title, and file system path.
MessageBox(hwnd, lpBuffer, "Display name", MB_OK);
if (SHGetPathFromIDList(pidlBrowse, lpBuffer))
SetWindowText(hwnd, lpBuffer);
// Free the PIDL returned by SHBrowseForFolder.
g_pMalloc->lpVtbl->Free(g_pMalloc, pidlBrowse);
}
// Clean up.
g_pMalloc->lpVtbl->Free(g_pMalloc, pidlPrograms);
g_pMalloc->lpVtbl->Free(g_pMalloc, lpBuffer);
}