小弟使用CFileDialog 文件对话框来选择多个文件。
CFileDialog filedlg(TRUE);
filedlg.m_ofn.lpstrFilter="SUZHOU MAJIAN files(*.mjmain)\0*.mjmain\0\0";
filedlg.m_ofn.lpstrTitle="选择主表(例如xx_TO_xxMain.mjmain)";我想加入默认的文件名
CString strfile;
strfile.Format("%s","xxxxx.mjmain");
于是使用 filedlg.m_ofn.lpstrFile=strfile.GetBuffer(100);
strfile.ReleaseBuffer();
这样用有问题,DoModal()后可以出来文件名,可是关闭的时候就出错了。使用char strfile[100];
strcpy(stfile,"xxxxx.mjmain");
filedlg.m_ofn.lpstrFile=stfile;
这样情况也是一样
请问一下这个应该怎么做?应为我会使用这个CFileDialog选择多个文件

解决方案 »

  1.   

    将文件路径复制到lpstrFile里面,不要直接用指针赋值。
      

  2.   

    看MSDN
    lpstrFile 
    Pointer to a buffer that contains a filename used to initialize the File Name edit control. The first character of this buffer must be NULL if initialization is not necessary. When the GetOpenFileName or GetSaveFileName function returns successfully, this buffer contains the drive designator, path, filename, and extension of the selected file. 
    If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the filenames of the selected files. For Explorer-style dialog boxes, the directory and filename strings are NULL separated, with an extra NULL character after the last filename. For old-style dialog boxes, the strings are space separated and the function uses short filenames for filenames with spaces. You can use theFindFirstFile function to convert between long and short filenames. If the buffer is too small, the function returns FALSE and the CommDlgExtendedError function returns FNERR_BUFFERTOOSMALL. In this case, the first two bytes of the lpstrFile buffer contain the required size, in bytes or characters. 所以不能在用完之前释放缓冲区
    TCHAR strFile[MAX_PATH+1] = {"xxxxx.mjmain"); 
    filedlg.m_ofn.lpstrFile = strFile;
    if( filedlg.DoModal() == IDOK)
    {
     ...........
    }
      

  3.   

    When the user allocates their own buffer to accommodate OFN_ALLOWMULTISELECT, the buffer can't be larger than 2048 or else everything gets corrupted (2048 is the maximum size).估计是你选的文件太多,超过了那个buffer搜索一下 蒋晟 的blog,里面有讲怎么让CFileDialog支持更大buffer的