none
利用SHFILEOPSTRUCT复制文件出现的问题 RRS feed

  • 常规讨论

  • SHFILEOPSTRUCT op1;
     char buf_to[]="C:\\新建文件夹\\";
     op1.hwnd=NULL;  
     op1.wFunc=FO_COPY;   
      op1.fFlags=FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION;
    char buf_form[]="C:\\文件夹1\\*";
      op1.pFrom=buf_form;  
     op1.pTo=buf_to; 
     ::SHFileOperation(&op1);
    上面这段代码没有问题,执行成功,但是如果我改成
    CString copyfrom="C:\\文件夹1\\*";
    strcpy(buf_form,copyfrom);
    则会出现错误,错误出现的原因应该是利用strcpy函数得到的buf_form是不对的,我要怎么做才能把CString类型的值赋给buf_form才能正确呢,我试过getbuffer也不对


    阿荣
    2009年3月30日 9:56

全部回复

  • 参数类型不对。参数不应该是字符串,而应该是用两个\0终止的缓冲区。参考SHFILEOPSTRUCT结构的文档
    如果你用CString,你应该将其内容复制到自己的缓冲区。

    MSMVP VC++
    2009年3月30日 15:43
    版主
  • Please not that the pFrom and pTo member of the SHFILEOPSTRUCT must be double-null terminated. That means you need to add an extra '\0' to the end of your string. Plase allocate one char more for your buf_from and buf_to.

    Please refer following code:

        CString strFrom(_T("C:\\somedir\\*"));
        CString strTo(_T("C:\\otherdir"));
    
        SHFILEOPSTRUCT option = {0};
        option.hwnd = NULL;
        option.wFunc = FO_COPY;
        option.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION;
    
        int nBufSize = strFrom.GetLength() + 2;
        LPTSTR pBufFrom = new TCHAR[nBufSize];
        ::SecureZeroMemory(pBufFrom, nBufSize * sizeof(TCHAR));
        StringCchCopy(pBufFrom, nBufSize, strFrom);
        option.pFrom = pBufFrom;
    
        nBufSize = strTo.GetLength() + 2;
        LPTSTR pBufTo = new TCHAR[nBufSize];
        ::SecureZeroMemory(pBufTo, nBufSize * sizeof(TCHAR));
        StringCchCopy(pBufTo, nBufSize, strTo);
        option.pTo = pBufTo;
    
        ::SHFileOperation(&option);
    2009年4月13日 11:52