Microsoft KB Archive/179372

{|
 * width="100%"|

FIX: Common File Dialog Multiple Selection File Limit

 * }

Q179372

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK), used with:
 * the operating system: Microsoft Windows NT 4.0

-

SYMPTOMS
When you use the GetOpenFileName API or the MFC CFileDialog class with the OFN_ALLOWMULTISELECT flag on Windows NT 4.0, you are limited to the number of characters that will be returned in the file name buffer.

CAUSE
The cause is in the common file dialog box code in Comdlg32.dll in Windows NT 4.0 and Windows NT 4.0 with Service Pack 1.

RESOLUTION
Install Windows NT 4.0 Service Pack 2 or later.

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Windows NT 4.0 Service Pack 2 or later.

MORE INFORMATION
The GetOpenFileName API takes a pointer to an OPENFILENAME structure that specifies options and initialization information for the Windows common file dialog box, and returns information about the file(s) selected. The MFC CFileDialog class encapsulates the Windows common file dialog box. The CFileDialog class's m_ofn member variable is a structure of type OPENFILENAME. You can specify the OFN_ALLOWMULTISELECT flag in the Flags member of the OPENFILENAME structure to allow the user to select more than one file.

When the OFN_ALLOWMULTISELECT and OFN_EXPLORER flags are specified, the lpstrFile member of the OPENFILENAME structure is a pointer to a caller- allocated buffer that receives the current directory followed by the names of the selected files. Each member of this list is separated by a single NULL value and the list is terminated by two sequential NULL values. If the OFN_EXPLORER flag is not specified, then the strings are space separated and the function adds the short (8.3) filenames to the list. The nMaxFile member specifies the size, in bytes (ANSI version) or characters (Unicode version), of the buffer pointed to by lpstrFile.

The GetOpenFileName or CFileDialog.DoModal function will return IDCANCEL if the length of the formatted string (composed of the directory and file names) exceeds any limit you set. If the user tries to open a number of files that will exceed the character limit, CommDlgExtendedError will return the value FNERR_BUFFERTOOSMALL. If this occurs, the first two bytes of lpstrFile contain the required buffer size in either bytes (ANSI version) or characters (Unicode version).

There is a bug in Windows NT 4.0 that limits the number of characters that will be copied to lpstrFile to 2,562 characters no matter what size is specified in nMaxFile. When this occurs, the function returns IDOK, but the file list in lpstrFile is truncated at this limit.

Steps to Reproduce Behavior
The following MFC code demonstrates the problem by trying to open various numbers of files:

  #include "cderr.h" //for definition of FNERR_BUFFERTOOSMALL

CFileDialog  dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL ); DWORD MAXFILE = 2562; //2562 is the max dlg.m_ofn.nMaxFile = MAXFILE; char* pc = new char[MAXFILE]; dlg.m_ofn.lpstrFile = pc; dlg.m_ofn.lpstrFile[0] = NULL;

int iReturn = dlg.DoModal; if(iReturn == IDOK) {     int nCount = 0; POSITION pos = dlg.GetStartPosition; while (pos != NULL) {        dlg.GetNextPathName(pos); nCount++; }     CString str; str.Format("Successfully opened %d files\n", nCount); AfxMessageBox(str); }  else if(iReturn == IDCANCEL) AfxMessageBox("Cancel");

if(CommDlgExtendedError == FNERR_BUFFERTOOSMALL) AfxMessageBox("BUFFERTOOSMALL"); delete []pc;