Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WriteFile returning zero and GetLastError returning 997(ERROR_IO_PENDING)

WriteFile returning zero and GetLastError returning 997(ERROR_IO_PENDING)

Scheduled Pinned Locked Moved C / C++ / MFC
helpjsontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    learningvisualc
    wrote on last edited by
    #1

    Hi all, I am trying to write to a port using CreateFile() and WriteFile() API's, but my WriteFile() is returning zero and on calling GetLastError() its returning error code 997 ERROR_IO_PENDING. I Know how to read a file when this happens but i am not getting how to write the file. Can anybody help me in doing this... Here is the code

    serial_handle = CreateFile(port_arg, GENERIC_READ | GENERIC_WRITE,
    0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    success = WriteFile(serial_handle ,&temp,5,&temp1,&overlappRead);

    I am not getting how to move ahead in this case Thanks in advance

    C X 2 Replies Last reply
    0
    • L learningvisualc

      Hi all, I am trying to write to a port using CreateFile() and WriteFile() API's, but my WriteFile() is returning zero and on calling GetLastError() its returning error code 997 ERROR_IO_PENDING. I Know how to read a file when this happens but i am not getting how to write the file. Can anybody help me in doing this... Here is the code

      serial_handle = CreateFile(port_arg, GENERIC_READ | GENERIC_WRITE,
      0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
      success = WriteFile(serial_handle ,&temp,5,&temp1,&overlappRead);

      I am not getting how to move ahead in this case Thanks in advance

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      WriteFile OVERLAPPED Structure See: If the function fails, or is completing asynchronously, the return value is zero (FALSE) NoteThe GetLastError code ERROR_IO_PENDING is not a failure; it designates the write operation is pending completion asynchronously. So your Write operation may be pending, so try checking 'Internal' member of OVERLAPPED structure or do Wait on its 'hEvent' member.

      1 Reply Last reply
      0
      • L learningvisualc

        Hi all, I am trying to write to a port using CreateFile() and WriteFile() API's, but my WriteFile() is returning zero and on calling GetLastError() its returning error code 997 ERROR_IO_PENDING. I Know how to read a file when this happens but i am not getting how to write the file. Can anybody help me in doing this... Here is the code

        serial_handle = CreateFile(port_arg, GENERIC_READ | GENERIC_WRITE,
        0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
        success = WriteFile(serial_handle ,&temp,5,&temp1,&overlappRead);

        I am not getting how to move ahead in this case Thanks in advance

        X Offline
        X Offline
        Xequen
        wrote on last edited by
        #3

        If serial_handle was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation. If serial_handle was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed. The former is asynchoronous and the latter is synchoronous. about your question,you can invoke "GetOverlappedResult( HANDLE hFile, // handle to file, pipe, or device LPOVERLAPPED lpOverlapped, // overlapped structure LPDWORD lpNumberOfBytesTransferred, // bytes transferred BOOL bWait // wait option );" this function to get result of WriteFile(); eg: success = WriteFil(serial_handle ,&temp,5,&temp1,&overlappRead); if(!success) { DWORD dwErrorCode = GetLastError(); switch(dwErrorCode) { case ERROR_IO_PENDING://IO is operating { GetOverlappedResult(serial_handle,&overlapped,&temp1,TRUE); //infinitely wait for WriteFile() operation compelete break; } //others error code default: { break } } }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups