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
.
What did I miss?
MSDN: aSegmentArray
...system memory page and must be aligned on a system memory page size boundary. Use _aligned_malloc()
instead of stack memory.