PRB: Common Dialog Open: Err=20476 Buffer lpstrFile Too Small

Last reviewed: February 18, 1996
Article ID: Q110185
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SYMPTOMS

The following behavior occurs with the Open file dialog box of the Common Dialog custom control when you set the Flags property to &H200& or OFN_ALLOWMULTISELECT, to allow selection of more than one file at once.

If you highlight more than about 20 files at once in the Open file dialog box, the following error message displays when you click the OK button:

   The buffer at which the member lpstrFile points is too small.

Or, if error trapping is on, the Err function reports error number 20476.

In the Visual Basic environment, if you press F5 or choose Continue from the Run menu after getting this error, you get the following error message:

   Invalid filename.

CAUSE

This behavior occurs when the length of the Open dialog string containing the file names exceeds 256, the default MaxFileSize property value.

RESOLUTION

To resolve the problem, increase the MaxFileSize property to 2048:

   CMDialog1.MaxFileSize = 2048

2048 is the maximum string size for the Common Dialog File-Open box. 256 is the default.

STATUS

This behavior is by design.

MORE INFORMATION

You can set the Flags property of the Common Dialog to &H200&, or OFN_ALLOWMULTISELECT, to allow multiple selections in the File Name list box. You can select more than one file at run time by pressing the SHIFT key and using the UP and DOWN ARROW keys to select the desired files. The FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

    If the common dialog CMDIALOG.VBX file is not automatically loaded by AUTOLOAD.MAK in Visual Basic, load CMDIALOG.VBX as follows: choose Add File from the File menu, and select CMDIALOG.VBX from your WINDOWS\SYSTEM directory.

  2. Add a Common Dialog custom control to Form1.

  3. Add the following to the Form Load event code:

       Sub Form_Load ()
          CMDialog1.Flags = &H200&   ' Allows selection of more than 1 file.
          CMDialog1.Filter = "ALL|*.*"   ' File types to list in file box.
          ' CMDialog1.MaxFileSize = 2048  ' Add this to correct the behavior.
          CMDialog1.Action = 1      ' Action 1 = standard Open file dialog box
       End Sub
    
    

  4. Start the program (or press F5). Select 20 or files at once by selecting the first article then pressing SHIFT+END. That highlights all files down to the last article. Choose the OK button. The following error message displays if the number of characters in all the selected file names, plus one character for each file name, exceeds 256:

          The buffer at which the member lpstrFile points is too small.
    

    If error trapping (On Error Goto) is on, the Err function reports error number 20476. In the Visual Basic environment, if you press F5 or choose Continue from the Run menu after getting this error, you get the following error message:

          Invalid filename.
    

To improve this behavior, add the CMDialog1.MaxFileSize=2048 statement before setting the CMDialog1 Action property to 1. This increases the maximum allowed returned string size from 256 to 2048.

REFERENCES

The following corrections apply to the "Language Reference":

  • On page 363 for the MaxFileSize property, change the "Applies to:" section to read "Common dialog (file dialogs)." Also say that the maximum MaxFileSize property value is 2048.
  • On page 89, add MaxFileSize and FilterIndex to the Common Dialog Control "Properties (File Dialogs)" section.


Additional reference words: 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsCus


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: February 18, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.