Original Post
I've been playing with asynchronouse file I/O on *nix and it seems to work pretty well, but I'm having trouble getting it to work on Windows. It seems like no matter how much data I try to read with ReadFile() it still behaves synchronously. When I try to read in around 200 MB ReadFile() fails with an ERROR_NO_SYSTEM_RESOURCES error. Here's some code: I have very little experience with Win32 programming, so I have no idea if all the flags and whatnot are set correctly. Anyway, in this particular bit of code, I always see "Opened file!" then maybe a pause, then "Success! bytesread=???" and then immediately "finished! bytesRead = ???" (the ??? is an actual number). I never see "working..." printed at all. Anyone see what I'm doing wrong?
#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
HANDLE file = CreateFile("setup.exe.part", FILE_READ_DATA,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
if (file == INVALID_HANDLE_VALUE)
cout << "Unable to open file!" << endl;
else
cout << "Opened file!" << endl;
OVERLAPPED o;
o.Offset = 0;
o.OffsetHigh = 0;
o.hEvent = CreateEvent(NULL, true, false, NULL);
int size =100000000;
char* buffer = new char[size];
DWORD bytesRead;
bool r = ReadFile(file, buffer, size, &bytesRead, &o);
if (!r)
{
int error = GetLastError();
if (error != ERROR_IO_PENDING)
{
cout << "ERROR! " << GetLastError() << endl;
return 1;
}
}
else
cout << "Success! bytesread= " << bytesRead << endl;
while(!HasOverlappedIoCompleted(&o))
{
cout << "working..." << endl;
}
cout << "finished! bytesRead = " << bytesRead << endl;
delete[] buffer;
return 0;
}