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

Article translations Article translations
Article ID: 224342 - View products that this article applies to.
This article was previously published under Q224342
Expand all | Collapse all

On This Page

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;
   }
}
				

Properties

Article ID: 224342 - Last Review: July 11, 2005 - Revision: 1.2
APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition
Keywords: 
kbprb KB224342

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com