SHFileOperation / SHFILEOPSTRUCT [英] SHFileOperation/SHFILEOPSTRUCT

查看:169
本文介绍了SHFileOperation / SHFILEOPSTRUCT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将目录复制到新位置。所以我使用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;
}

所以我设置了上面的值。有一个文件,我创建的文件夹(我已经关闭句柄,所以应该很好移动)。 SHFileOperation调用返回2,但我不能找到解释这些错误代码的地方。有谁知道我可以找出什么2意味着,或任何人有任何想法,为什么它可能不工作? Cheers

解决方案

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

请参阅 Windows系统错误代码错误描述的完整列表,或写一个函数,将获得错误代码的描述:

  std :: string error_to_string(const DWORD a_error_code)
{
//获取最后一个Windows错误消息。
char msg_buf [1025] = {0};

//获取我们的os代码的错误信息。
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
0,
a_error_code,
0,
msg_buf,
sizeof(msg_buf) - 1,
0 ))
{
//删除后面的换行符。
char * nl_ptr = 0;
if(0!=(nl_ptr = strchr(msg_buf,'\\\
')))
{
* nl_ptr ='\0';
}
if(0!=(nl_ptr = strchr(msg_buf,'\r')))
{
* nl_ptr ='\0'
}

return std :: string(msg_buf);
}

return std :: string(无法获取错误消息);
}

从阅读 SHFileOperation pTo 指定的字符串, code>和 pFrom 必须是双空终止:yours只是单终止。尝试以下操作:

  dirName.append(1,'\0'); 
sf.pFrom = dirName.c_str();
string copyDir = homeDir +\\CopyDir;
copyDir.append(1,'\0');
sf.pTo = copyDir.c_str();


Im trying to copy a directory to a new location. So I am using SHFileOperation/SHFILEOPSTRUCT as follows:

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;
}

So I set the values as above. There is a file I created in the folder (I have closed the Handle so it should be fine to move). The SHFileOperation call is returning 2, but I cant find anywhere where these error codes are explained. Does anyone know where I can find out what 2 means, or does anyone have any ideas why it might not be working? Cheers

解决方案

Error code 2 means The system cannot find the file specified.

See Windows System Error Codes for full listing of error descriptions, or write a function that will obtain the description for the error code:

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");
}

From reading the documentation for SHFileOperation the strings specified for pTo and pFrom must be double null terminated: yours are only singly null terminated. Try the following:

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

这篇关于SHFileOperation / SHFILEOPSTRUCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆