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

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
Last Review : October 11, 2007
Revision : 1.2

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 STATUS_DISK_FULL error message.
• The value of the lpNumberOfBytesWritten parameter is set to 0 (zero).

Back to the top

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.

Back to the top

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 Symptoms section mentions. Use this method to work around the scenario in which the WriteFile function returns a STATUS_DISK_FULL error message.

Back to the top

MORE INFORMATION

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

http://msdn2.microsoft.com/en-us/library/aa365747.aspx (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 (http://msdn2.microsoft.com/en-us/library/aa365531.aspx)

Back to the top


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
• Windows Vista Home Premium 64-bit Edition
• Windows Vista Home Basic 64-bit Edition
• Windows Vista Starter
• Microsoft Windows Server 2003 Service Pack 1
• Microsoft Windows Server 2003 Service Pack 2, when used with:
    Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
    Microsoft Windows Server 2003, Standard Edition (32-bit x86)
    Microsoft Windows Server 2003, Enterprise x64 Edition
    Microsoft Windows Server 2003, Standard x64 Edition

Back to the top

Keywords: 
kbtshoot kbprb kbexpertiseinter KB935788

Back to the top

 

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image