Microsoft KB Archive/194929

{|
 * width="100%"|

BUG: CreateFile with OPEN_ALWAYS Sets Last Error Incorrectly

 * }

Q194929

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), used with:
 * Microsoft Windows NT Server version 4.0
 * Microsoft Windows NT Workstation version 4.0

-

SYMPTOMS
If you call the CreateFile API with the OPEN_ALWAYS flag when the specified file already exists, the API should return a valid handle and GetLastError should return ERROR_ALREADY_EXISTS.

When the file that is being opened is being accessed using either a UNC path name or through a mapped drive, GetLastError sometimes returns ERROR_SUCCESS even if the file already exists.

RESOLUTION
Break up the CreateFile call with the OPEN_ALWAYS flag into two separate calls to CreateFile.

Call CreateFile with the OPEN_EXISTING flag. If the call to CreateFile succeeds, the file already existed. If the call to CreateFile fails, you can call CreateFile with the OPEN_ALWAYS flag.

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

MORE INFORMATION
This error only occurs if the file is being accessed using either a UNC path name or through a mapped drive and the file is located on a Windows NT Server.

Additional query words:

Keywords : kbFileIO kbKernBase kbOSWinNT400 kbSDKPlatform kbDSupport kbGrpDSKernBase

Issue type : kbbug

Technology : kbAudDeveloper kbWin32sSearch kbWin32API