如何使用 SHFILEOPSTRUCT 移动多个文件?

发布于 2024-08-01 16:54:32 字数 1650 浏览 1 评论 0原文

如何使用 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]....

How to move multi files using 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);

Is there anything wrong?
It always say no XXX directory. XXX the dstPaths[0]....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-08-08 16:54:32

从表面上看,您的 pFrom 和 pTo 列表是错误的。

您需要将它们形成为每个之间都有一个 NULL 终止符,并在末尾有一个双 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.

原始代码中的主要问题是您没有注意每个文件名之间所需的空终止符。 您是否尝试过通过调试器运行您所拥有的内容并查看 FromBuf 包含的内容? 我怀疑如果你有的话,你很快就会发现这个问题。

希望有帮助!

By the look of it you are forming your pFrom and pTo lists wrongly.

You need to form them such that each has a NULL terminator between them and a double null terminator at the end.

An example re-implementation of your function would be:

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.

The main problems in your original code is that you are not payign attention to the required null terminators between each file name. Have you tried running what you have through the debugger and looking at what FromBuf contains? I suspect you would have seen the problem very quickly if you had.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文