Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

I am getting following errors when I use the CFileDialog to open some files.

Sometimes It gives an exception "buffer OverRun" after selecting the specified file.

Sometime produce exception during the DoModal.

Can you please let me know what is wrong with this piece of code.

C++
CFileDialog dlg(TRUE);
dlg.m_pOFN->lpstrFileTitle = _T("Open Token File");
dlg.m_pOFN->nMaxFile       = 511;
dlg.m_pOFN->lpstrFilter = _T("TokenFile(*.tkn)\0*.tkn\0\0"); 
   
CString szFilePath;

dlg.m_pOFN->lpstrFile   = szFilePath.GetBuffer(10000);
if(dlg.DoModal() == IDOK)
{
	//szFilePath = dlg.GetFileName();
	m_txtFileName.SetWindowText(szFilePath);

}

Thanks
Sutha
Posted
Updated 28-Feb-12 23:48pm

1 solution

Do it this way ommitting the OFN structure:
C++
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("TokenFile(*.tkn)|*.tkn||"));
if(dlg.DoModal() == IDOK)
{
	m_txtFileName.SetWindowText(dlg.GetFileName().GetString());
 
}

Your errors are:

  1. lpstrFileTitle is the buffer to receive the selected file name, not the title for the dialog.
  2. lpstrFilter can be also passed using the constructor. Note that '|' is used to separate the string.
  3. lpstrFile is the file name to initialize the edit control, not the buffer to receive it. It may be also passed using the constructor.
  4. UPDATE: When using GetBuffer() with CString, you must call ReleaseBuffer().
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900