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

删除回忆录丶 提交于 2019-12-02 12:48:29

问题


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

回答1:


I forgot to specify FILE_FLAG_OVERLAPPED to CreateFile.




回答2:


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 :)



来源:https://stackoverflow.com/questions/16721864/why-is-the-callback-given-to-readfileex-not-receiving-the-correct-overlapped-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!