Re: WriteFile()
- From: "Chris Burnette" <burnette@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 10 Apr 2005 01:01:58 -0400
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
>
.
- Follow-Ups:
- Re: WriteFile()
- From: Hector Santos
- Re: WriteFile()
- References:
- WriteFile()
- From: Frank A. Uepping
- Re: WriteFile()
- From: Hector Santos
- Re: WriteFile()
- From: Gary Chanson
- WriteFile()
- Prev by Date: Re: WriteFile()
- Next by Date: Re: WriteFile()
- Previous by thread: Re: WriteFile()
- Next by thread: Re: WriteFile()
- Index(es):
Relevant Pages
|
Loading