2009-08-11 7 views
0

SHFILEOPSTRUCT를 사용하여 여러 파일을 이동하는 방법은 무엇입니까?SHFILEOPSTRUCT를 사용하여 여러 파일을 이동하는 방법은 무엇입니까?

  vector<CString> srcPaths; 
      vector<CString> dstPaths; 
    SHFILEOPSTRUCT FileOp;//定义SHFILEOPSTRUCT结构对象; 
    int fromBufLength = MAX_PATH * imageVec.size() + 10; 

    TCHAR *FromBuf = new TCHAR[fromBufLength]; 
    TCHAR *ToBuf = new TCHAR[fromBufLength]; 

    shared_array<TCHAR> arrayPtr(FromBuf); 
    shared_array<TCHAR> arrayPtr2(ToBuf); 
    ZeroMemory(FromBuf, sizeof(TCHAR) * fromBufLength); 
    ZeroMemory(ToBuf, sizeof(TCHAR) * fromBufLength); 

    // 拼接移动自目录字符串 
    int location = 0; 
    TCHAR* tempBuf = FromBuf; 
    for (int i = 0; i < srcPaths.size(); ++i) 
    { 
     const CString& filePath = srcPaths[i]; 
     if (i != 0) 
     { 
      location ++; 
     } 
     tempBuf = FromBuf + location; 
     wcsncpy(tempBuf, (LPCTSTR)(filePath), filePath.GetLength()); 
     location += filePath.GetLength(); 
    } 
    // 拼接移动到目录字符串 
    location = 0; 
    tempBuf = ToBuf; 
    CString filePath; 
    for (int i = 0; i < dstPaths.size(); ++i) 
    { 
     filePath = dstPaths[i]; 
     if (i != 0) 
     { 
      location ++; 
     } 
     tempBuf = ToBuf + location; 
     wcsncpy(tempBuf, (LPCTSTR)(filePath), filePath.GetLength()); 
     location += filePath.GetLength(); 
    } 
    tempBuf = NULL; 

    FileOp.hwnd = NULL/*this->m_hWnd*/; 
    FileOp.wFunc=FO_MOVE; 
    FileOp.pFrom = FromBuf; 
    FileOp.pTo = ToBuf; 
    FileOp.fFlags = FOF_NOCONFIRMATION; 
    FileOp.hNameMappings = NULL; 
    int nOk=SHFileOperation(&FileOp); 

잘못된 것이 있습니까? 항상 XXX 디렉토리가 없다고 말합니다. XXX dstPaths [0] ....

답변

1

모양을 보면 pFrom과 pTo가 잘못 나열됩니다.

각 끝에 NULL 종결자가 있고 끝 부분에 이중 널 종결자를 포함하도록 구성해야합니다. 함수의

예를 다시 구현은 다음과 같습니다

TCHAR* tempBuf = FromBuf; 
for (int i = 0; i < srcPaths.size(); ++i) 
{ 
     const CString& filePath = srcPaths[i]; 
     _tcscpy_s(tempBuf, fromBufLength, filePath.GetString()); 
     tempBuf += filePath.GetString() + 1; // Include null terminator in the increment. 
} 
*tempBuf = '\0'; // Add extra null terminator. 

원래 코드의 주요 문제는 각 파일 이름 사이에 필요한 널 터미네이터에 관심을 payign하지 않는 것입니다. 디버거를 통해 무엇을 가지고 있는지 실행 해보고 FromBuf에 포함 된 것을 살펴 보셨습니까? 당신이 가진다면 당신이 그 문제를 매우 빨리 보았을 거라고 생각합니다.

희망 하시겠습니까?

관련 문제