From: Vanja on
Hello,

Does someone know why the CFileDialog throws CInvalidArgException if
"recycled" (DoModal called the second time)? The problem happens only on
Vista und Windows 7, and only if using bVistaSyle==TRUE. Creating the dialog
with bVistaStyle==FALSE solves the problem, but it's a poor solution. Also,
if file filters are not used, no exception occurs.

Here's the code (more or less the same example like in MSDN):

// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, NULL, 0,
/*bVistaStyle*/ TRUE);

// first time -> ok
fileDlg.DoModal();

// second time -> not so ok (CInvalidArgException thrown)
// If the dialog was created with szFilters==NULL, no problems.
// If the dialog was created with bVistaStyle==FALSE, no problems.
fileDlg.DoModal();


Stack at the point of exception (first-chance) is:

mfc90ud.dll!AfxThrowInvalidArgException() Line 233
mfc90ud.dll!CFileDialog::ApplyOFNToShellDialog() Line 388
mfc90ud.dll!CFileDialog::DoModal() Line 706
CommonDialog_Test.exe!CCommonDialog_TestDlg::OnBnClickedBtnOpentwice()
Line 142
. . . .

I could easily circumvent the problem by always creating a fresh instance of
CFileDialog. But since the file-browser is required many times in my
application, my intention was to create it once, re-use it and thus improve
the reaction time.

Thanks in advance.
From: Alexandre Grigoriev on
Creation of CFileDialog is very inexpensive operation. You don't gain
anything by reusing it. DoModal itself is much more expensive.

"Vanja" <Vanja(a)discussions.microsoft.com> wrote in message
news:BEF69A5E-CD82-46DB-A62B-9154164791CD(a)microsoft.com...
> Hello,
>
> Does someone know why the CFileDialog throws CInvalidArgException if
> "recycled" (DoModal called the second time)? The problem happens only on
> Vista und Windows 7, and only if using bVistaSyle==TRUE. Creating the
> dialog
> with bVistaStyle==FALSE solves the problem, but it's a poor solution.
> Also,
> if file filters are not used, no exception occurs.
>
> Here's the code (more or less the same example like in MSDN):
>
> // szFilters is a text string that includes two file name filters:
> // "*.my" for "MyType Files" and "*.*' for "All Files."
> TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");
>
> // Create an Open dialog; the default file name extension is ".my".
> CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
> OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, NULL, 0,
> /*bVistaStyle*/ TRUE);
>
> // first time -> ok
> fileDlg.DoModal();
>
> // second time -> not so ok (CInvalidArgException thrown)
> // If the dialog was created with szFilters==NULL, no problems.
> // If the dialog was created with bVistaStyle==FALSE, no problems.
> fileDlg.DoModal();
>
>
> Stack at the point of exception (first-chance) is:
>
> mfc90ud.dll!AfxThrowInvalidArgException() Line 233
> mfc90ud.dll!CFileDialog::ApplyOFNToShellDialog() Line 388
> mfc90ud.dll!CFileDialog::DoModal() Line 706
> CommonDialog_Test.exe!CCommonDialog_TestDlg::OnBnClickedBtnOpentwice()
> Line 142
> . . . .
>
> I could easily circumvent the problem by always creating a fresh instance
> of
> CFileDialog. But since the file-browser is required many times in my
> application, my intention was to create it once, re-use it and thus
> improve
> the reaction time.
>
> Thanks in advance.