首页 > 代码库 > WriteFile操作时返回87错误

WriteFile操作时返回87错误

调用 WriteFile写数据时出错,用GetLastError得到错误代码为87(无效参数)。 

代码片段如下:

  if ((!WriteFile(
        m_hDevEx,
        pBuf,
        size,
        &WriteBytes,
        &Overlapped)) &&
        (GetLastError() != ERROR_IO_PENDING))
    {
        OutputDebugString("Write error\n");
        return false;

    }

 问题出在最后一个参数Overlapped,初始化是这样的:

    OVERLAPPED Overlapped;
    Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
这种初始化在ReadFile时时没有问题的,而WriteFile时需要将该结构体中的Offset,OffsetHigh两个参数设置成零,即:

    OVERLAPPED    Overlapped;
    Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    Overlapped.Offset = 0;
    Overlapped.OffsetHigh = 0; 
以上这样初始化之后,错误消失!