如何使用 SHFILEOPSTRUCT 移动多个文件?
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从表面上看,您的 pFrom 和 pTo 列表是错误的。
您需要将它们形成为每个之间都有一个 NULL 终止符,并在末尾有一个双 null 终止符。
重新实现函数的示例如下:
原始代码中的主要问题是您没有注意每个文件名之间所需的空终止符。 您是否尝试过通过调试器运行您所拥有的内容并查看 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:
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!