Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateFile( ... CREATE_NEW, ... )

2 views
Skip to first unread message

Neil Rashbrook

unread,
Mar 5, 2008, 11:52:18 AM3/5/08
to
The documentation for GetTempFileName has some strange warnings
regarding OEM/ANSI, so I thought it would be simple enough to write my
own version using CreateFile. When I use CreateFile with the CREATE_NEW
creation distribution, I typically run into one of the following scenarios:

1. Success (ERROR_SUCCESS)
2. A file already exists (ERROR_ALREADY_EXISTS)
3. No permissions to create a new file (ERROR_ACCESS_DENIED)

However I have since discovered that if there happens to be a directory
with the name I'm trying to create then I also get ERROR_ACCESS_DENIED
in that case. Surely I should expect ERROR_ALREADY_EXISTS? This makes
the whole point of CREATE_NEW somewhat useless. Is GetTempFileName
likely to suffer from the same bug, after all it presumably has to use
CreateFile to create the new file?

Sam Hobbs

unread,
Mar 5, 2008, 4:17:42 PM3/5/08
to
"Neil Rashbrook" <ne...@parkwaycc.co.uk> wrote in message
news:eUiBKFuf...@TK2MSFTNGP03.phx.gbl...

> The documentation for GetTempFileName has some strange warnings regarding
> OEM/ANSI

You can probably ignore that.

> However I have since discovered that if there happens to be a directory
> with the name I'm trying to create then I also get ERROR_ACCESS_DENIED in
> that case. Surely I should expect ERROR_ALREADY_EXISTS?

If there is a directory with the same name, then the file does not and
cannot exist.

> Is GetTempFileName likely to suffer from the same bug, after all it
> presumably has to use CreateFile to create the new file?

The bug is your design. It is impossible for a file and a directory to have
the same name. If you read the GetTempFileName documentation and use
GetTempFileName to benefit from all it does, you can get it to do most
everything for you.

Neil Rashbrook

unread,
Mar 6, 2008, 7:52:17 AM3/6/08
to
Sam Hobbs wrote:

>"Neil Rashbrook" <ne...@parkwaycc.co.uk> wrote in message news:eUiBKFuf...@TK2MSFTNGP03.phx.gbl...
>
>

>>However I have since discovered that if there happens to be a directory with the name I'm trying to create then I also get ERROR_ACCESS_DENIED in that case. Surely I should expect ERROR_ALREADY_EXISTS?
>>
>>
>If there is a directory with the same name, then the file does not and cannot exist.
>

I don't care that the file does not exist. I want to know that the file
cannot be created for reasons not related to access permissions.

>>Is GetTempFileName likely to suffer from the same bug, after all it presumably has to use CreateFile to create the new file?
>>
>>
>The bug is your design.
>

It turns out that it is, but for a different reason - it turns out I'm
using the same internal function to create file names for backup files
and temporary files. And for backup files it's good to know that the
original file is really a directory. But for temporary files the
suggested file name isn't really relevant.

>It is impossible for a file and a directory to have the same name.
>

That's alright, I don't want it to have the same name as a directory.
But I do want to know that I can't create the file because it would have
the same name, whether as a directory or as an existing file.

>If you read the GetTempFileName documentation and use GetTempFileName to benefit from all it does, you can get it to do most everything for you.
>

Actually there's one thing GetTempFileName can't do, and that's to use
the suggested file name if it's available, which was the behaviour it
turns out I wanted all along (no point creating a backup file name for
the original version).

Ben Voigt [C++ MVP]

unread,
Mar 6, 2008, 11:47:51 AM3/6/08
to

"Neil Rashbrook" <ne...@parkwaycc.co.uk> wrote in message
news:eUiBKFuf...@TK2MSFTNGP03.phx.gbl...

Now that you've discovered this "feature", it should be trivial to handle
it. Just call GetFileAttributes with the same name to learn whether there
is a directory there.


0 new messages