PRB: SHBrowseForFolder() Fails When Entering an Invalid Folder Name

ID: Q224342


The information in this article applies to:


SYMPTOMS

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.


CAUSE

The SHBrowseForFolder() function returns NULL if an invalid folder name is typed into the optional edit control.


RESOLUTION

To allow only a valid folder to be returned from the SHBrowseForFolder() function, use an application-defined callback function and the BIF_VALIDATE flag.


STATUS

This behavior is by design.


MORE INFORMATION

Steps to Reproduce Behavior

The following Win32 application demonstrates how to use the BrowseCallbackProc() function to validate a folder name that is typed into the optional edit box in a Browse Folder dialog box.

To demonstrate the problem that occurs when an invalid folder name is entered, remove the comment characters from the #define SHOW_PROBLEM line:

#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