Search code examples
cwinapivisual-c++file-iooverlapped-io

Why is the callback given to ReadFileEx() not receiving the correct OVERLAPPED structure?


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);
    }
}

Solution

  • I forgot to specify FILE_FLAG_OVERLAPPED to CreateFile.