BUG: CreateFile with OPEN_ALWAYS Sets Last Error Incorrectly

ID: Q194929

The information in this article applies to:

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 kbNTOS400 kbSDKPlatform 
Issue type        : kbbug
Solution Type     : kbpending

Last Reviewed: October 31, 1998