Microsoft KB Archive/224342

{|
 * width="100%"|

PRB: SHBrowseForFolder Fails When Entering an Invalid Folder Name

 * }

Q224342

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK)

-

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.

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 
 * 1) include 

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 ; bi.lpfn = BrowseCallbackProc; bi.lParam = NULL; bi.iImage = 0;
 * 1) ifndef SHOW_PROBLEM
 * 1) endif

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


 * 1) 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 kbOSWin2000 kbSDKWin32 kbGrpDSShell

Issue type : kbprb

Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch