Microsoft KB Archive/935788

= Data is written to a file on a network share even though the WriteFile function returns a value of 0 for the lpNumberOfBytesWritten parameter when you run a custom application in Windows Vista or Windows Server 2003 SP1 or a later version =

Article ID: 935788

Article Last Modified on 10/11/2007

-

APPLIES TO

 Windows Vista Ultimate Windows Vista Enterprise Windows Vista Business Windows Vista Home Premium Windows Vista Home Basic Windows Vista Ultimate 64-bit Edition Windows Vista Enterprise 64-bit Edition Windows Vista Business 64-bit Edition</li> Windows Vista Home Premium 64-bit Edition</li> Windows Vista Home Basic 64-bit Edition</li> Windows Vista Starter</li> Microsoft Windows Server 2003 SP1</li> Microsoft Windows Server 2003 Service Pack 2, when used with: <ul> Microsoft Windows Server 2003, Enterprise Edition</li></ul>

<ul> Microsoft Windows Server 2003, Standard Edition (32-bit x86)</li></ul>

<ul> Microsoft Windows Server 2003, Enterprise x64 Edition</li></ul>

<ul> Microsoft Windows Server 2003, Standard x64 Edition</li></ul> </li></ul>

-

<div class="symptoms_section">

SYMPTOMS
Consider the following scenario. You create a custom application on a computer that is running Windows Vista or Microsoft Windows Server 2003 with Service Pack (SP1) or a later version. The custom application opens a file on a network share for write access. Additionally, the custom application uses the WriteFile function to write data to the file. When you run the custom application, you experience the following symptoms:
 * The WriteFile function returns an error message.
 * The WriteFile function returns a value of 0 (zero) for the lpNumberOfBytesWritten parameter.

You expect that the write operation failed and that no data is written to the file. However, you find that some data is successfully written to the file.

The following is an example of a scenario in which you experience this behavior.
 * The custom application opens a file for an unbuffered write operation. The WriteFile function extends the file.

Note In certain cases, the write operation may be unbuffered even though the application does not specifically set the FILE_FLAG_NO_BUFFERING flag when the application opens the file. For example, if you open the file for write-only access instead of for read and write access, write operations to the file are unbuffered.
 * Part of the write operation fails because the disk is full. However, some disk space is freed on the server. Later parts of the write operation are successfully completed.

In this scenario, the following results occur:
 * The file contains blank spaces.
 * The WriteFile function returns a &quot;STATUS_DISK_FULL&quot; error message.
 * The value of the lpNumberOfBytesWritten parameter is set to 0 (zero).

<div class="cause_section">

CAUSE
This behavior may occur if the following conditions are true:
 * The write operation is split into multiple Server Message Block (SMB) requests. If the data is more than 64 kilobytes (KB), the SMB redirector breaks the data into multiple SMB requests.
 * The server returns errors for some requests but not for other requests.

When an error occurs during a write operation, the WriteFile function always returns a value of 0 (zero) for the lpNumberOfBytesWritten parameter. However, in scenarios in which part of the write operation fails and part of the write operation is successfully completed, some data is written to the file.

<div class="workaround_section">

WORKAROUND
To work around this behavior, set the file attributes to reflect the extended size of the file before you use the WriteFile function to extend the file. To do this, you can use the SetEndOfFile function.

Important This workaround applies only to the example scenario that the &quot;Symptoms&quot; section mentions. Use this method to work around the scenario in which the WriteFile function returns a &quot;STATUS_DISK_FULL&quot; error message.

<div class="moreinformation_section">

MORE INFORMATION
For more information about the WriteFile function, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/aa365747.aspx

For more information about the SetEndOfFile function, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/aa365531.aspx

Additional query words: I/O protocol API

Keywords: kbtshoot kbprb kbexpertiseinter KB935788

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.