cFileDialog DoModal异常 [英] cFileDialog DoModal Exception

查看:300
本文介绍了cFileDialog DoModal异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人

使用CFileDialog打开某些文件时出现以下错误.

有时,选择指定的文件后,它会给出异常缓冲区溢出".

有时会在DoModal期间产生异常.

您能否让我知道这段代码有什么问题.

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.

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);

}


谢谢
Sutha


Thanks
Sutha

推荐答案

以这种方式忽略OFN结构:
Do it this way ommitting the OFN structure:
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是用于接收所选文件名(而不是对话框标题)的缓冲区.
  2. lpstrFilter也可以使用构造函数传递.请注意,"|"用于分隔字符串.
  3. lpstrFile是用于初始化编辑控件的文件名,而不是用于接收它的缓冲区.
  4. 更新:将GetBuffer()CString一起使用时,必须调用ReleaseBuffer().

  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().


这篇关于cFileDialog DoModal异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆