SHFileOperation错误87

时间:2017-03-02 12:04:51

标签: c++ winapi shfileoperation

我正在尝试将A驱动器的内容复制到文件夹C:\ test \ disk1中。文件夹disk1已存在。程序编译但是当它运行时我得到错误87.我知道错误87与无效参数有关但我不确定问题所在。有没有人有任何想法?

#include <Windows.h>
#include <stdio.h>

int main(int argc, char ** argv)
{
    const wchar_t *const sourceFile = L"A:\\";
    const wchar_t *const outputFile = L"C:\\test\\disk1";

    SHFILEOPSTRUCTW fileOperation;
    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW));

    fileOperation.wFunc = FO_COPY;
    fileOperation.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR |
                               FOF_NOERRORUI | FOF_FILESONLY;
    fileOperation.pFrom = sourceFile;
    fileOperation.pTo = outputFile;

    int result = SHFileOperationW(&fileOperation);
    if (result != 0)
    {
        printf("SHFileOperation Failure: Error%u\n", result);
        return 1;
    }

    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW));

    printf("OK\n");
    return 0;
}

1 个答案:

答案 0 :(得分:3)

请注意SHFILEOPSTRUCT的文档,特别是pFrompTo的文档:

  PCZZTSTR     pFrom;
  PCZZTSTR     pTo;

PCZZTSTR是什么意思?

pFrom
Type: PCZZTSTR
Note  This string must be double-null terminated.

所以你的修复是提供额外的尾随零。

const wchar_t *const sourceFile = L"A:\\\0";
const wchar_t *const outputFile = L"C:\\test\\disk1\0";

请注意,Windows API函数接受/作为目录分隔符,因此可以更容易阅读:

const wchar_t *const sourceFile = L"A:/\0";
const wchar_t *const outputFile = L"C:/test/disk1\0";

PCZZSTR实际上是一个指向零终止字符串列表的指针,该字符串以空字符串结束。)