gpt4 book ai didi

c++ - SHFileOperation/SHFILEOPSTRUCT

转载 作者:可可西里 更新时间:2023-11-01 09:46:05 25 4
gpt4 key购买 nike

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

所以我设置了上面的值。文件夹中有一个我创建的文件(我已经关闭了 handle ,所以应该可以移动)。 SHFileOperation 调用返回 2,但我找不到解释这些错误代码的任何地方。有谁知道我在哪里可以找到 2 的意思,或者有谁知道为什么它可能不起作用?干杯

最佳答案

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

参见 Windows System Error Codes获取错误描述的完整列表,或编写一个函数来获取错误代码的描述:

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

来自阅读 SHFileOperation 的文档为 pTopFrom 指定的字符串必须是双空终止的:你的只是单空终止的。尝试以下操作:

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

关于c++ - SHFileOperation/SHFILEOPSTRUCT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com