如题,昨天遇到一个产品中的清空功能不可用的问题。现象也比较奇怪:当点击界面上的清空历史列表功能的时候,要删除的目标文件夹有时能被删除,有时却不能被删除。经过调试,确认问题出在 SHFileOperation 的调用上,它有时会返回错误码:1026 (0x402 )。先看下这个API的原型:
int SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
其中结构体 LPSHFILEOPSTRUCT 的定义为:
经过一番谷歌,终于查到原因:调这个 API 时传入的结构体的成员 pFrom 必须以两个 '/0' 字符结尾,而不论有没有指定 fFlags 为 FOF_MULTIDESTFILES!
嗐,原来是这个原因!
原来是 MSDN 没看清楚,写代码时没有多加一个 '/0'。下面是 MSDN 的部分说明:
pFrom
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: