我正在尝试使用SHFileOperation将文件/目录从一个位置复制到另一位置。
到目前为止,我的代码是
for(int index=0; index < clbSource.GetCount();index++ )
{
SHFILEOPSTRUCT SHFileOp;
CString src,dest;
std::string NULLTermination="\\0\\0";
//Get source file/directory path from CListBox
int n = clbSource.GetTextLen(index);
clbSource.GetText(index, src.GetBuffer(n));
src.ReleaseBuffer();
CT2CA pszConvertedAnsiString (src);
std::string strStdS (pszConvertedAnsiString);
//replace single slash with double
size_t found = 0, next = 0;
while( (found = strStdS.find('\\', next)) != std::string::npos )
{
strStdS.insert(found, "\\");
next = found+4;
}
//Add double null termination
strStdS.append(NULLTermination);
std::wstring stempS = s2ws(strStdS);
//Get source file/directory path from CListBox
int m = clbDest.GetTextLen(index);
clbDest.GetText(index, dest.GetBuffer(n));
dest.ReleaseBuffer();
CT2CA pszConvertedAnsiStringd (dest);
std::string strStdD (pszConvertedAnsiStringd);
//replace single slash with double
size_t foundD = 0, nextD = 0;
while( (foundD = strStdD.find('\\', nextD)) != std::string::npos )
{
strStdD.insert(foundD, "\\");
nextD = foundD+4;
}
//Add double null termination
strStdD.append(NULLTermination);
std::wstring stempD = s2ws(strStdD);
// set up File Operation structure
ZeroMemory(&SHFileOp, sizeof(SHFILEOPSTRUCT));
// application the owner of the progress dialog.
SHFileOp.hwnd = GetSafeHwnd();
SHFileOp.wFunc = FO_COPY;
SHFileOp.pFrom = stempS.c_str();
//SHFileOp.pFrom = _T("E:\\backup\\java softwares\0\0");
SHFileOp.pTo = stempD.c_str();
//SHFileOp.pTo = _T("E:\\Tmp\0\0");
// Set the flags.
SHFileOp.fFlags = FOF_SIMPLEPROGRESS;
// Do it.
DWORD result = SHFileOperation(&SHFileOp);
//Check for success
if(result!=0)
{
::AfxMessageBox(ERR_MSG_FAILED_TO_COPY);
}
else
::AfxMessageBox(SUCCESS);
}
现在,结果为非零,即124。
也通过此链接System Error 124 - ERROR_INVALID_LEVEL with SHFileOperation。
无法理解问题所在。
注意:如果将硬编码的值(如图所示)传递给'SHFileOp.pFrom'和'SHFileOp.pTo',则可以正常使用。
谁能帮忙?
最佳答案
SHFileOperation
由于“历史原因”不返回标准Windows错误代码。
MSDN:SHFileOperation,在“返回值”下有一个可能结果的列表。
124 == 0x7C ==源或目标中的路径或两者均无效。
检查您实际上在结构中传递的值,路径转换中可能存在错误。
关于c++ - 错误124- SHFileOperation的系统调用级别不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32738935/