Microsoft KB Archive/139972

= FIX: File Open Common Dialog in Win95 Returns Invalid Filename =

Article ID: 139972

Article Last Modified on 11/15/2006

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Windows 95

-



This article was previously published under Q139972



SYMPTOMS
Calling the ShowOpen method of the common dialog control to show the File Open common dialog box results in the following error:

Run-time error '20477'

Invalid filename



CAUSE
This error occurs the second time the ShowOpen method is called after multiple file names have been previously selected in the File Open dialog box. The only time this error occurs is when the Flags property of the common dialog box includes both cdlOFNAllowMultiselect (to allow multiple selections) and cdlOFNExplorer (to use the Windows Explorer File Open dialog box). Without the new shell, Microsoft Windows NT does not display the Windows Explorer Open File dialog box, so this problem only occurs with Microsoft Windows 95.

In this case, clicking multiple files in the File Open dialog box and clicking Open causes the FileName property of the common dialog control to be filled with a NULL delimited string of filenames. When the ShowOpen method is called, an attempt is made to set the default file(s) to whatever the FileName property of the common dialog control is. The error results because the common dialog control does not know what to do when it encounters embedded NULLs in the FileName property.



RESOLUTION
NULLs must be used instead of spaces to delimit the list of file names because the Windows Explorer style Open File common dialog box with multiple selections enabled automatically includes support for long filenames. However, because the Open File dialog box does not accept a list of NULL delimited files when it is displayed, there is no way to have multiple files selected by default when displaying the Open File dialog box.

Use either of the following methods to avoid the error described above:

In your code, before the ShowOpen method is called again, reset the FileName property of the common dialog box to a single file or to no file at all.

- or -

Do not use the cdlOFNAllowMultiselect and cdlOFNExplorer flags at the same time. Search on "CommonDialog control," Topic: "Changes to CommonDialog Flags for Windows 95 and Windows NT," in the Visual Basic Help file for more information on the behavior of different combinations of these constants under different operating systems.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of the article. This problem has been fixed in Microsoft Windows 98.



Steps to reproduce problem (under Windows 95 only)
 Start Visual Basic, or if it is already running, click New Project on the File menu. In the toolbox, double-click CommonDialog to add a common dialog control to the form. In the toolbox, double-click the command button to add a new command button to the form.  Add the following code to the Command1_Click procedure: Private Sub Command1_Click CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer CommonDialog1.ShowOpen End Sub

 On the Run menu, click Start or press the F5 key to run your application.</li> Click the command button and in the resulting File Open dialog box select two or more files. Click Open to hide the File Open dialog box.</li> Click the command button again to attempt to show the File Open dialog box again. Instead of the dialog box, a message box showing the above mentioned error is displayed.</li></ol>

<div class="references_section">