Microsoft KB Archive/842792

= An &quot;ERROR_ACCESS_DENIED&quot; error occurs when you try to write to a file that is in a network shared folder =

Article ID: 842792

Article Last Modified on 1/17/2007

-

APPLIES TO

 Microsoft Windows XP Professional, when used with:  Microsoft Windows Server 2003, Standard Edition (32-bit x86)

 Microsoft Windows 2000 Standard Edition  Microsoft Windows XP Home Edition, when used with:  Microsoft Windows Server 2003, Standard Edition (32-bit x86)

 Microsoft Windows 2000 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

Important This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

<div class="symptoms_section">

SYMPTOMS
A call to the WriteFile function may not succeed. You experience this symptom when the following conditions are true:
 * You are using a client computer that is running Microsoft Windows XP Service Pack 1 (SP1).
 * You use the WriteFile function to try to write to a file that is in a network shared folder.
 * The shared folder is on a server computer that is running either Microsoft Windows 2000 or Microsoft Windows Server 2003.

Additionally, you may receive the following error message:

ERROR_ACCESS_DENIED

<div class="cause_section">

CAUSE
This problem occurs when the following conditions are true:
 * When you used the CreateFile function to create the file, you specified only the GENERIC_WRITE constant in the dwDesiredAccess parameter.
 * Server Message Block (SMB) signing is enabled for communication between the client computer and the server computer.

<div class="workaround_section">

WORKAROUND
To work around this problem, use either of the following methods:
 * Specify the GENERIC_READ constant and the GENERIC_WRITE constant in the dwDesiredAccess parameter.
 * Modify SMB signing.

Specify the GENERIC_READ constant and the GENERIC_WRITE constant in the dwDesiredAccess parameter
Use the following code to create the file.

Note Replace the following placeholders:
 * Replace the  placeholder with the name of a server computer that is running Windows 2000 or Windows Server 2003.
 * Replace the  placeholder with the name of a network shared folder on the server computer.

// Specify the GENERIC_READ constant and the GENERIC_WRITE constant // in the dwDesiredAccess parameter when you create the file. hFile = CreateFile(&quot;\\\\ServerName\\FolderName\\Test.txt&quot;, GENERIC_READ |   GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

Modify SMB signing
Warning If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

Follow these steps on the server computer and on the client computer: <ol> In Registry Editor, locate and then click the following registry subkey:

</li> In the right pane of Registry Editor, right-click requiresecuritysignature in the Name field, and then click Modify. The Edit DWORD Value dialog box appears.</li> In the Value data box, type 0, and then click OK.</li> Quit Registry Editor, and then restart the computer.</li></ol>

<div class="moreinformation_section">

Steps to reproduce the behavior
<ol> On a computer that is running Windows XP SP1, use Microsoft Visual C++ 6.0 to create a simple Win32 Console Application project that is named Test. By default, the Test.cpp file is created.</li> Create a file by specifying only the GENERIC_WRITE constant in the dwDesiredAccess parameter when you use the CreateFile function.</li>  Use the WriteFile function to try to write to the file. To do this, replace the existing code in the Test.cpp file with the following code.

Note Replace the following placeholders:  Replace the  placeholder with the name of a server computer that is running Windows 2000 or Windows Server 2003.</li> Replace the  placeholder with the name of a network shared folder on the server computer.</li></ul>


 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;windows.h&quot;
 * 3) include &quot;stdio.h&quot;
 * 4) include &quot;conio.h&quot;

void main {   HANDLE hFile; char lpBuffer[99999]; DWORD lpNumberOfBytesWritten;

// Write data to the buffer that you will // you use to write data to the file. for (int i = 0; i < 100000; ++i) lpBuffer[i] = 'a';

// Specify only the GENERIC_WRITE constant in the // dwDesiredAccess parameter when you create the file. hFile = CreateFile(&quot;\\\\ServerName\\FolderName\\Test.txt&quot;,       GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

// Try to write the data in the buffer to the file. // If the call to the WriteFile function fails, // call the GetLastError function. if (!WriteFile(hFile, lpBuffer, 100000, &lpNumberOfBytesWritten, NULL))

// If an ERROR_ACCESS_DENIED error has occurred, inform the user. if (GetLastError == ERROR_ACCESS_DENIED) {           printf(&quot;An ERROR_ACCESS_DENIED error has occurred.&quot;); printf(&quot;Press any key to continue.&quot;); getch; }

// Close the handle to the file. CloseHandle(hFile); } </li> Build and then run the application. A console window appears. If the behavior that is mentioned in the &quot;Symptoms&quot; section occurs, the console window contains the following output:

An ERROR_ACCESS_DENIED error has occurred.

Press any key to continue.

</li></ol>

<div class="references_section">