Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WriteFile lpNumberOfBytesWritten less than nNumberOfBytesToWrite

When calling WriteFile can the returned lpNumberOfBytesWritten ever be less than nNumberOfBytesToWrite in synchronous write mode where an error has not occured (return is TRUE)? Writing is done to a proper file (not a socket, pipe, or other special handle). lpOverlapped is not used.

It is not entirely clear from the docs.

like image 454
edA-qa mort-ora-y Avatar asked Mar 25 '11 06:03

edA-qa mort-ora-y


People also ask

Why does the WriteFile function fail with error_not_enough_quota?

The WriteFile function may fail with ERROR_NOT_ENOUGH_QUOTA, which means the calling process's buffer could not be page-locked. For more information, see SetProcessWorkingSetSize. If part of the file is locked by another process and the write operation overlaps the locked portion, WriteFile fails.

Why does WriteFile return false when using anonymous pipe?

For more information, see CreateFile and Synchronous and Asynchronous I/O . If an anonymous pipe is being used and the read handle has been closed, when WriteFile attempts to write using the pipe's corresponding write handle, the function returns FALSE and GetLastError returns ERROR_BROKEN_PIPE .

What is the size of the WriteFile pipe in Linux?

For x64 platforms it's 31.97 MB. For Itanium it's 63.95 MB. For more information regarding pipes, see the Remarks section. A pointer to the variable that receives the number of bytes written when using a synchronous hFile parameter. WriteFile sets this value to zero before doing any work or error checking.

How to get the number of bytes a file has been written?

If the hFile parameter is associated with an I/O completion port, you can also get the number of bytes written by calling the GetQueuedCompletionStatus function. In Windows Server 2012, this function is supported by the following technologies.


1 Answers

The documentation states:

The WriteFile function returns when one of the following conditions occur:

  • The number of bytes requested is written.
  • A read operation releases buffer space on the read end of the pipe (if the write was blocked). For more information, see the Pipes section.
  • An asynchronous handle is being used and the write is occurring asynchronously.
  • An error occurs.

Only the first one of these meets your criteria so the answer is that the documentation is clear *lpNumberOfBytesWritten is always equal to nNumberOfBytesToWrite when the function returns when called as you specify.

like image 77
David Heffernan Avatar answered Oct 26 '22 17:10

David Heffernan