none
WriteFile does not return error when I tried to write to offset beyond the volume of hard drive RRS feed

  • Question

  • void

    Block_File_Win::write_file_overlapped(uint64_toffset, conststd::vector<char>& data, uint32_tcount)

    {

          COverlappedoverlap(offset);

          if(FALSE== WriteFile(m_hFile, data.data(), count, NULL, overlap.GetOverlapped()))

          {

              if(GetLastError() == ERROR_IO_PENDING)

              {

                    if(WAIT_OBJECT_0!= WaitForSingleObject(overlap.GetOverlapped()->hEvent, INFINITE))

                        throwstd::exception("write_block: failed to wait for writing completion");

              }

              else

                    throwstd::exception("write_block: failed to WriteFile");

          }

    }

    I CreateFile with FILE_FLAG_OVERLAPPED flag and created an Event object for writing. I set the Offset and OffsetHigh as

    a very large value (33553172539392 in bytes), which is much larger than my hard disk. The WriteFile

    returned FALSE and GetLastError() returned ERROR_IO_PENDING as I expected. But WaitForSingleObject returned

    immediately with a WAIT_OBJECT_0 which means writing completed, that's obviously impossible. After that, I checked my file on disk,

    the size of file is still zero not surprisingly.

    The offset I passed for WriteFile is obviously invalid, so should I know the writing operation actually failed in this case?

    Wednesday, August 31, 2016 4:00 PM

Answers

All replies

  • Here is how I created the file and how I create an Event

    m_hFile

    = CreateFile(file_name.c_str(),

    GENERIC_READ| GENERIC_WRITE,

    FILE_SHARE_READ| FILE_SHARE_WRITE,

    NULL,

    dwCreationDisposition,

    FILE_FLAG_OVERLAPPED| FILE_FLAG_RANDOM_ACCESS,

    NULL);


    memset(&m_overlap, 0, sizeof(m_overlap));

    m_overlap.Offset= DWORD(offset);

    m_overlap.OffsetHigh= DWORD(offset>> (8 * sizeof(DWORD)));

    m_overlap.hEvent= CreateEvent(NULL, TRUE, FALSE, NULL);

    Wednesday, August 31, 2016 4:07 PM
  • Actually you are not testing for whether an error is returned.  You need to do more after the WaitForSingleObject, you then need GetOverlappedResult and another GetLastError see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365690(v=vs.85).aspx for an example.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Wednesday, August 31, 2016 9:52 PM
  • Many thanks for your explanation! I then replaced WaitForSingleObject with GetOverlappedResult with bWait flag set as TRUE. I got FALSE returned which meant something wrong occurred during WriteFile. ^_^, I was very happy to see the error.
    Thursday, September 1, 2016 1:18 PM