调用 SHFileOperation 删除文件夹时返回 1026 (0x402)错误的原因
2010-06-23 12:26
483 查看
如题,昨天遇到一个产品中的清空功能不可用的问题。现象也比较奇怪:当点击界面上的清空历史列表功能的时候,要删除的目标文件夹有时能被删除,有时却不能被删除。经过调试,确认问题出在 SHFileOperation 的调用上,它有时会返回错误码:1026
(0x402
)。先看下这个API的原型:
int SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
其中结构体 LPSHFILEOPSTRUCT 的定义为:
经过一番谷歌,终于查到原因:调这个 API 时传入的结构体的成员 pFrom 必须以两个 '/0' 字符结尾,而不论有没有指定 fFlags 为
FOF_MULTIDESTFILES![/code]
[/code]
(0x402
)。先看下这个API的原型:
int SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
其中结构体 LPSHFILEOPSTRUCT 的定义为:
] typedef struct _SHFILEOPSTRUCT { HWND hwnd; UINT wFunc; LPCTSTR pFrom; LPCTSTR pTo; FILEOP_FLAGS fFlags; BOOL fAnyOperationsAborted; LPVOID hNameMappings; LPCTSTR lpszProgressTitle; } SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
经过一番谷歌,终于查到原因:调这个 API 时传入的结构体的成员 pFrom 必须以两个 '/0' 字符结尾,而不论有没有指定 fFlags 为
FOF_MULTIDESTFILES![/code]
嗐,原来是这个原因!
原来是 MSDN 没看清楚,写代码时没有多加一个 '/0'。下面是 MSDN 的部分说明:
pFrom Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom .
下面是一个更正后的 Demo:
#include "stdafx.h" #include <Windows.h> #include <string> #include <iostream> HRESULT RemoveDir( std::wstring& wstrTargetPath ) { if (wstrTargetPath.empty() || wstrTargetPath.length() > MAX_PATH) { return E_INVALIDARG; } /* * 元素全部初始化为 '/0',保证结尾至少有两个 '/0' */ WCHAR pwszTargetPath[MAX_PATH + 1] = {0}; ::wcsncpy_s( pwszTargetPath, _countof(pwszTargetPath), wstrTargetPath.c_str(), wstrTargetPath.length() ); SHFILEOPSTRUCTW FileOp = { NULL, FO_DELETE, pwszTargetPath, NULL, FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION, FALSE, NULL, NULL }; int nRet = ::SHFileOperationW(&FileOp); return HRESULT_FROM_WIN32(nRet); } int _tmain(int argc, _TCHAR* argv[]) { std::wcout.imbue(std::locale("chs")); std::wstring wstrPathToRemove(L"C://Documents and Settings//All Users//Application Data//TestDir"); HRESULT hRet = RemoveDir( wstrPathToRemove ); if (SUCCEEDED(hRet)) { std::wcout << L"Remove directory/"" << wstrPathToRemove << L"/" succeeded." << std::endl; } else { std::wcout << L"Remove directory/"" << wstrPathToRemove << L"/" failed with error: 0x" << std::hex << hRet << L"." << std::endl; } return 0; }
[/code]
相关文章推荐
- 微信红包接口调用后返回异常错误原因
- 调用 RegLoadKey 失败,返回错误码1314的原因及解决办法
- perl 调用rm -rf 递归删除指定文件夹,提示 can not remove: not empty 原因
- 对 COM 组件的调用返回了错误 HRESULT E_FAIL。
- asp.net Girdview 批量删除错误的原因
- vc使用SHFileOperation()的使用对文件夹的复制,删除,移动,重命名
- "对COM 组件的调用返回了错误 HRESULT E_FAIL"的解决方法
- .net定期删除一文件夹下的所有文件,并调用相关exe
- Hessain调用expected end of map ('z') at '?'错误原因
- SharePoint 2010 爬网错误 对 COM 组件的调用返回了错误 HRESULT E_FAIL。 (Error from SharePoint site: 无法完成此操作)
- 解决AE中GP工具错误 对 COM 组件的调用返回了错误 HRESULT E_FAIL
- ArcEngine对COM组件的调用返回了错误 HRESULT E_FAIL
- Android开发 Fragment中调用startActivityForResult返回错误的requestCode
- SHFileOperation()函数移动,复制,删除文件\文件夹
- Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL
- SQL2008 新建维护计划 调用的目标发生了异常 从 IClassFactory 为 CLSID 为 {....} 的 COM 组件创建实例失败,原因是出现以下错误:c001f011.(Micros
- 由于某种错误导致数据表数据巨大(千万级别),删除后对该数据操作速度极慢的原因及解决方法
- 调用Ajax返回500错误的解决方法
- AE二次开发 COM 组件的调用返回了错误 HRESULT E_FAIL
- 微信,QQ,分享后没有调用监听回调的错误原因