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

This article was previously published under Q224342
This article has been archived. It is offered "as is" and will no longer be updated.
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_PROBLEMint 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 1int 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: 01/10/2015 11:50:13 - Revision: 1.2

  • Microsoft Platform Software Development Kit-January 2000 Edition
  • kbnosurvey kbarchive kbprb KB224342
Feedback