SHFileOperation/SHFILEOPSTRUCT

dis*_*ney 2 c++ windows

我试图将目录复制到新位置.所以我使用SHFileOperation/SHFILEOPSTRUCT如下:

SHFILEOPSTRUCT sf;
memset(&sf,0,sizeof(sf));
sf.hwnd = 0;
sf.wFunc = FO_COPY;
dirName += "\\*.*";
sf.pFrom = dirName.c_str();
string copyDir = homeDir + "\\CopyDir";
sf.pTo = copyDir.c_str();
sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;

int n = SHFileOperation(&sf);
if(n != 0)
{
    int x = 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我将值设置如上.我在文件夹中创建了一个文件(我关闭了Handle,所以移动应该没问题).SHFileOperation调用返回2,但我无法找到解释这些错误代码的任何地方.有谁知道我在哪里可以找到2意味着什么,或者有没有人有任何想法为什么它可能无法正常工作?干杯

hmj*_*mjd 7

错误代码2表示系统找不到指定的文件.

有关错误说明的完整列表,请参阅Windows系统错误代码,或编写将获取错误代码说明的函数:

std::string error_to_string(const DWORD a_error_code)
{
    // Get the last windows error message.
    char msg_buf[1025] = { 0 };

    // Get the error message for our os code.
    if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 
                      0,
                      a_error_code,
                      0,
                      msg_buf,
                      sizeof(msg_buf) - 1,
                      0))
    {
        // Remove trailing newline character.
        char* nl_ptr = 0;
        if (0 != (nl_ptr = strchr(msg_buf, '\n')))
        {
            *nl_ptr = '\0';
        }
        if (0 != (nl_ptr = strchr(msg_buf, '\r')))
        {
            *nl_ptr = '\0';
        }

        return std::string(msg_buf);
    }

    return std::string("Failed to get error message");
}
Run Code Online (Sandbox Code Playgroud)

从阅读的文档SHFileOperation指定的字符串pTopFrom必须是双空终止:你只是单独空终止.请尝试以下方法:

dirName.append(1, '\0');
sf.pFrom = dirName.c_str();
string copyDir = homeDir + "\\CopyDir";
copyDir.append(1, '\0');
sf.pTo = copyDir.c_str();
Run Code Online (Sandbox Code Playgroud)