Question

For some reason, my callback isn't receiving the address of the correct OVERLAPPED structure after a call to ReadFileEx. What can cause this?

Update -- example:

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

void __stdcall completion_routine(
    unsigned long dwErrorCode,
    unsigned long dwNumberOfBytesTransfered,
    OVERLAPPED *lpOverlapped)
{
    printf("Overlapped = %p\n", lpOverlapped);
}

int _tmain(int argc, LPTSTR argv[])
{
    HANDLE hvolume = CreateFile(
        _T("C:\\Windows\\Notepad.exe"), FILE_READ_DATA,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        NULL, OPEN_EXISTING, 0, NULL);
    char tempbuf[512];
    OVERLAPPED tempoverlapped = { };
    printf("%p\n", &tempoverlapped);
    if (ReadFileEx(hvolume, tempbuf, sizeof(tempbuf),
                   &tempoverlapped, &completion_routine)
        && GetLastError() == ERROR_SUCCESS)
    {
        SleepEx(INFINITE, TRUE);
    }
}
Was it helpful?

Solution

I forgot to specify FILE_FLAG_OVERLAPPED to CreateFile.

OTHER TIPS

Also, it's also quite possible that ownership of the original OVL struct was not relinquished when the callback was set up, and so overwritten between the setup and the callback.

Or even possible that an inexperienced developer might allocate it on the stack of the thread setting it up - not a good move :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top