Re: WriteFile()



The original question was whether WriteFile could ever return TRUE and have
nNumberOfBytesToWrite > *lpNumberOfBytesWritten

The documentation on MSDN for WriteFile clearly states:
When writing to a nonblocking, byte-mode pipe handle with insufficient
buffer space, WriteFile returns TRUE with *lpNumberOfBytesWritten <
nNumberOfBytesToWrite

Futhermore, this can happen if someone call SetCommTimeouts and the serial
port cannot write the data fast enough and times out. Try setting the
WriteTotalTimeoutConstant to 10 and the WriteTotalTimeoutMultiplier to 0 and
then try writing 1MB to a comport. Since it can't send 1MB across that fast,
WriteFile times out and returns the number of bytes it was able to transmit.
In this case, WriteFile still returns a success value (the same thing
happens on a read timeout). From my perspective, this is expected... you've
told it to timeout and it did... no error occurred.

I would also think that the following is a plausible situation.:
You have a disk that has 1MB left on it.
You call WriteFile with a 2MB buffer.
WriteFile returns true with 1MB written.
The next call to WriteFile returns false with a disk full error.

I tried verifying this using a USB key (didn't want to fill up my hard
drive); however, the above situation didn't happen. WriteFile failed (disk
full error) without filling up my key (there was free space left). This
might lead me to believe that when using WriteFile to write to storage
media, it does some additional checking to see if it can write the entire
contents of the buffer before doing it. However, this could be unique to
using the USB key and may depend on the file system, drivers, and operating
system involved.

Typically, I've found that WriteFile will report that it's written the same
number of bytes that you've told it to write. I would think that an
application should not necessarily assume that this is the case; these
return values are in there for a reason.

Chris


"Gary Chanson" <gchanson@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:ecm4YpTPFHA.3716@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Hector Santos" <nospamhere@xxxxxxxxxxxxxx> wrote in message
> news:O2xlWhPPFHA.1096@xxxxxxxxxxxxxxxxxxxxxxx
>> Chris said it was possible.
>>
>> Unless he knows something unbeknownst to me, I don't think so. I can't
>> think of any normal condition where this is possible.
>
> I can't think of any either.
>
> --
> -GJC [MS Windows SDK MVP]
> -Software Consultant (Embedded systems and Real Time Controls)
> - http://www.mvps.org/ArcaneIncantations/consulting.htm
> -gchanson@xxxxxxxx
>


.



Relevant Pages

  • Re: writing a small portion of file triggers full file rearangemen
    ... FILE_OBJECT lock and prevent your WriteFile from proceeding. ... I carefully extend the file with SetEndOfFile. ... Sometimes my problem is observed when writing into first ... Don Burn (MVP, Windows DDK) ...
    (microsoft.public.win32.programmer.kernel)
  • Re: WriteFile()
    ... I'm only writing based on my experience, ... expectations that you MUST NEVER use a single call to WriteFile because you ... DWORD nAmount) // number of bytes to write ... Unrealistic in the real world of File I/O and communications design. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: WriteFile()
    ... I'm only writing based on my experience, ... expectations that you MUST NEVER use a single call to WriteFile because you ... DWORD nAmount) // number of bytes to write ...
    (microsoft.public.win32.programmer.kernel)
  • Re: WriteFile()
    ... The original question was whether WriteFile could ever return TRUE and have ... nNumberOfBytesToWrite> *lpNumberOfBytesWritten ... When writing to a nonblocking, ... I tried verifying this using a USB key (didn't want to fill up my hard ...
    (microsoft.public.win32.programmer.kernel)
  • WriteFile reports x bytes written, but file does not grow
    ... cut and merge WAV files. ... Now, the Web Service and the COM object work fine on my XP machine, writing ... This 1 kB is from a previous WriteFile action that writes a WAV ... So, after writing the wave header, every call to writefile does not result ...
    (microsoft.public.vc.language)

Loading