ID: Q179372
The information in this article applies to:
- Microsoft Windows NT version 4.0
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.
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.
Install Windows NT 4.0 Service Pack 2 or greater.
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
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.
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;
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
(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 : kbcode kbCmnDlg kbCmnDlgFileO kbCmnDlgSave kbMFC kbNTOS400bug kbGrpUser
Issue type : kbbug
Last Reviewed: January 2, 1999