BUG: Common File Dialog Multiple Selection File Limit

Last reviewed: January 15, 1998
Article ID: Q179372
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - 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 greater.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

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;

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q99097
   TITLE     : HOWTO: Customize Common Dialog Box Parameter Blocks

   ARTICLE-ID: Q162160
   TITLE     : DOC: CFileDialog::DoModal Does Not Return 0

   ARTICLE-ID: Q131225
   TITLE     : PRB: CFileDialog::DoModal() Does Not Display FileOpen Dialog

   ARTICLE-ID: Q115087
   TITLE     : HOWTO: Change the Background Color of a Common Dialog

   ARTICLE-ID: Q130761
   TITLE     : HOWTO: Use FileOpen Common Dialog w/OFN_ALLOWMULTIPLESELECT

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Philip Spory, Microsoft Corporation


Additional query words: CFileDialog OFN_ALLOWMULTISELECT OPENFILENAME
nMaxFile lpstrFile Common File Dialog limit max GetOpenFileName
Keywords : UsrCmnDlg kbcode
Technology : kbMfc
Version : WINNT:4.0
Platform : winnt
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.