PRB: SHBrowseForFolder() Fails When Entering an Invalid Folder NameID: Q224342
|
When a folder name is typed into the optional edit control in the Browse Folders dialog box and OK is clicked, the SHBrowseForFolder() function returns NULL and the pszDisplayName buffer is not set.
The SHBrowseForFolder() function returns NULL if an invalid folder name is typed into the optional edit control.
To allow only a valid folder to be returned from the SHBrowseForFolder() function, use an application-defined callback function and the BIF_VALIDATE flag.
This behavior is by design.
#include <windows.h>
#include <shlobj.h>
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg,
LPARAM lParam, LPARAM lpData);
// To demonstrate the problem, remove the comment characters from the
// following line:
//#define SHOW_PROBLEM
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst,
LPSTR lpszCmdLine, int nCmdShow)
{
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
TCHAR szDisplayName[MAX_PATH];
szDisplayName[0] = '\0';
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = TEXT("Browsing");
bi.ulFlags = BIF_EDITBOX | BIF_VALIDATE ;
#ifndef SHOW_PROBLEM
bi.lpfn = BrowseCallbackProc;
#endif
bi.lParam = NULL;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (NULL != pidl)
MessageBox(NULL, szDisplayName, TEXT("SHBrowseFolder"), MB_OK);
else
MessageBox(NULL, TEXT("SHBrowseFolder failed"), TEXT("SHBrowseFolder"), MB_OK);
return 0;
}
#define DONT_DISMISS 1
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg,
LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_VALIDATEFAILED:
return DONT_DISMISS;
default:
return 0;
}
}
Additional query words:
Keywords : kbLib kbSDKWin32 kbGrpShell
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 27, 1999