CFileDialog打开多个文件失败 返回错误 FNERR_BUFFERTOOSMALL

需要使用CFileDialog,产生一个文件多选的对话框。但是默认的lpstrFile缓冲区只有200多,当打开比较多的文件时容易超过缓冲区返回错误:FNERR_BUFFERTOOSMALL 由CommDlgExtendedError()返回。

查看MSDN解释:

ValueMeaning
FNERR_BUFFERTOOSMALLThe buffer pointed to by the lpstrFile member of the OPENFILENAME structure is too small for the filename specified by the user. The first two bytes of the lpstrFile buffer contain an integer value specifying the size, in bytes (ANSI version) or 16-bit characters (Unicode version), required to receive the full name.


那就手动将缓冲区设置大一点即可。缓冲区的上限是2562。看下面示例:

 

char *  lpFile;   
CFileDialog FileDlg(TRUE,NULL,NULL,NULL,   
        
" Media Files (*.avi;*.mkv)|*.avi;*.mkv|All Files (*.*)|*.*|| " );   
  
FileDlg.m_ofn.nMaxFile
= 2562 ;      
lpFile
= new   char [ 2562 ];      
FileDlg.m_ofn.lpstrFile
= lpFile;      
FileDlg.m_ofn.lpstrFile[
0 ] = NULL;   
  
FileDlg.m_ofn.Flags 
|=  OFN_ALLOWMULTISELECT  |  OFN_EXPLORER;   // 设置可多选文件标志   
FileDlg.m_ofn.lStructSize = 88 ;   // 设置EXPLORER风格   
  
if  (FileDlg.DoModal() == IDOK)   
   {   
        CString strFilePath
= "" ;   
        POSITION p
= FileDlg.GetStartPosition();   
        
while  (p)   
        {   
             strFilePath
= FileDlg.GetNextPathName(p);    
             
// 处理每个文件路径   
         }   
   }
   delete[] lpFile;

  

还可以参考codeproject上的实例代码:

链接:http://www.codeproject.com/KB/dialog/pja_multiselect.aspx

转载于:https://www.cnblogs.com/ciey/archive/2009/08/06/1540550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值