My goal is to read dataBuffer from A and write the data to device B repeatedly using MSDN APIs. I am using ReadFileEx and WriteFileEx to request overlapped I/O reading and writing, so that the original time-consuming for-loop using ReadFile and WriteFile can now go background and the UI main thread can be responsive all the time.
I first create structure MY_OVERLAPPED which inherits from OVERLAPPED and two more elements are added, event is a pointer to MyClass which can help access the variables in that class, handles, dataBuffer, etc., and count is for resetting Offset value each time before ReadFileEx is called. The reading position is count*524288, since I read 524288 bytes each time and store in dataBuffer.
My question is: 1.how do I not use the inherited OVERLAPPED structure's Offset element to change reading position, such as using reference count?
2.The following code does not work the way I think. The ReadCompletionRoutine can only run one time and end up with receiving extremely long offset number. For now I presume that's the issue and focus on it first, so please feel free to correct any of my code structure.
My code flow is: 1. set reading point to 0 2. call overlapped ReadFileEx 3. ReadCompletion will be called, the data read will be written using overlapped WriteFileEx 4. WriteCompletion will be called, set pointer to next 524288 bytes and read 5.(and so on) two completion routines will call each other till all data is transfered
MyClass.h
struct MY_OVERLAPPED: OVERLAPPED {
MyClass *event;
unsigned long long count;
};
MyClass.cpp - main
MY_OVERLAPPED overlap;
memset(&overlap, 0,sizeof(overlap));
//point to this class (MyClass), so all variables can later be accessed
overlap.event = this;
//set read position******how do I not use Offset??
overlap.Offset = 0;
overlap.OffsetHigh = 0;
overlap.count = 0;
//start the first read io request, read 524288 bytes, which 524288 bytes will be written in ReadCompletionRoutine
ReadFileEx(overlap.event->hSource, overlap.event->data, 524288, &overlap, ReadCompletionRoutine);
SleepEx(0,TRUE);
MyClass.cpp - CALLBACKs
void CALLBACK ReadCompletionRoutine(DWORD errorCode, DWORD bytestransfered, LPOVERLAPPED lpOverlapped)
{
//type cast to MY_OVERLAPPED
MY_OVERLAPPED *overlap = static_cast<MY_OVERLAPPED*>(lpOverlapped);
//write 524288 bytes and continue to read next 524288 bytes in WriteCompletionRoutine
WriteFileEx(overlap->event->hDevice, overlap->event->data, 524288, overlap, WriteCompletionRoutine);
}
void CALLBACK WriteCompletionRoutine(DWORD errorCode, DWORD bytestransfered, LPOVERLAPPED lpOverlapped)
{
MY_OVERLAPPED *overlap = static_cast<MY_OVERLAPPED*>(lpOverlapped);
//set new offset to 524288*i, i = overlap->count for next block reading
overlap->count = (overlap->count)+1;
LARGE_INTEGER location;
location.QuadPart = 524288*(overlap->count);
overlap->Offset = location.LowPart;
overlap->OffsetHigh = location.HighPart;
ReadFileEx(overlap->event->hSource, overlap->event->data, 524288, overlap, ReadCompletionRoutine);
}