Re: CFileDialog failure
"Giovanni Dicanio" <giovanni.dicanio@invalid.it> ha scritto nel messaggio
news:ObaBbsWEIHA.5976@TK2MSFTNGP02.phx.gbl...
This is a refactored version, which does not allocate the dialog class on
the heap, as Alexander's post pointed out:
<code>
//------------------------------------------------------------------
// Tests Open file dialog in multi-select mode.
//------------------------------------------------------------------
void CTestDlg::OnBnClickedButton1()
{
// Max files in list (including folder path)
const int MaxFileCount = 50;
// Max TCHARs per file
const int MaxFilePath = MAX_PATH;
// Calculate approx size for the given number of max files.
// The "+1" is for the terminating NUL (_T('\0')) character.
const int MaxFileBuffer = MaxFileCount * MaxFilePath + 1;
//const int MaxFileBuffer = 30; // TEST for small buffer
// Buffer for file names
std::vector< TCHAR > fileNamesBuffer(MaxFileBuffer);
// File dialog object
CFileDialog fileDlg(
TRUE, // Open file dialog
NULL, // No default extension
NULL, // Initial file name
OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,
_T("All files (*.*)|*.*||"), // Filter
this // Parent
);
// Hook our buffer to file dialog structure
fileDlg.GetOFN().lpstrFile = &(fileNamesBuffer[0]);
fileDlg.GetOFN().nMaxFile = (DWORD) fileNamesBuffer.size();
bool okPressed = false;
while ( true )
{
// Clear the first two bytes of lpstrFile filed,
// so we don't see garbage from the previous iteration
// (if the previous iteration failed).
::ZeroMemory( fileDlg.GetOFN().lpstrFile, 2 );
// Run the dialog-box
INT_PTR result = fileDlg.DoModal();
// Check result
if ( result == IDCANCEL )
{
// Failure or user pressed Cancel?
DWORD dw = ::CommDlgExtendedError();
if ( dw & FNERR_BUFFERTOOSMALL )
{
// Failure due to few buffer memory;
// tell that to the user
AfxMessageBox( _T("Too many files selected.") );
// Do another iteration
continue;
}
// User pressed cancel, or other error.
// Quit loop.
break;
}
// User pressed OK
okPressed = true;
// Quit loop
break;
}
// Show selected files, if user pressed OK
if ( okPressed )
ProcessFileList( &(fileNamesBuffer[0]) );
}
</code>
Giovanni