简体   繁体   English

Windows WriteFileGather函数将最后一个错误设置为ERROR_INVALID_PARAMETER

[英]Windows WriteFileGather function set last error as ERROR_INVALID_PARAMETER

Example code to reproduce my problem: 示例代码重现我的问题:

#include <windows.h>
#include <iostream>

int main()
{
  using namespace std;
  HANDLE hdl = CreateFile("test.file", GENERIC_WRITE,
                          FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
                          FILE_ATTRIBUTE_NORMAL  |
                          FILE_FLAG_NO_BUFFERING |
                          FILE_FLAG_OVERLAPPED,
                          NULL);
  char data[4096] = {0};
  DWORD written = 0;

  for(size_t i = 0; i < sizeof(data); ++i)
    data[i] = 'a' + (i % 26);

  OVERLAPPED async_hdl = {0};
  FILE_SEGMENT_ELEMENT segs[3] = {0};

  segs[0].Buffer = PtrToPtr64(data);
  segs[1].Buffer = PtrToPtr64(data);

  if(!WriteFileGather(hdl, segs, 8192, NULL, &async_hdl)) {
    cout << "Last error: " << GetLastError() << endl;
    cout << "overlapped internal status: " << async_hdl.Internal << endl;
    // Last error: 87
    // overlapped internal status: 259
  }
  CloseHandle(hdl);
  return 0;
}

I compile it with Visual Studio 2012 and test on Windows 7. As inlined comment, the WriteFileGather function always sets the last error as ERROR_INVALID_PARAMETER (87) and Internal field of the OVERLAPPED struct is 259 . 我使用Visual Studio 2012对其进行编译并在Windows 7上进行测试。作为内联注释, WriteFileGather函数始终将最后一个错误设置为ERROR_INVALID_PARAMETER (87),并且OVERLAPPED结构的Internal字段为259

What did I miss? 我错过了什么?

MSDN: aSegmentArray ...system memory page and must be aligned on a system memory page size boundary. MSDN: aSegmentArray ...系统内存页,并且必须在系统内存页大小边界上对齐。 Use _aligned_malloc() instead of stack memory. 使用_aligned_malloc()而不是堆栈内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM