Some Win32 API that returns a handles specify that they
return INVALID_HANDLE_VALUE when they fail (e.g. CreateFile).
Some of them say they return NULL (e.g. CreateFileMapping).
In all the cases I'm interested in I free the handle with
CloseHandle(). Are there any cases where CreateFile()
can return a valid NULL handle and CreateFileMapping()
can return INVALID_HANDLE_VALUE and succeed?
thanks,
marco
Marco,
Since CreateFile is documented to return INVALID_HANDLE_VALUE, you
ought to assume that 0 (NULL) is a valid return. It's best to play
safe and work with the documentation. Even if you don't encounter a
problem now, you may in the future!
Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.
Oh, alright... I thought maybe it was one of those things
that is the way it is because of history. It makes it
hard to write an AutoHandleCloser that closes a handle
when it goes out of scope. (I suppose that I could damn
the torpedoes and close it anyway, but apparently it will
throw an exception in the debugger...)
thanks,
marco
If the above statement is TRUE. there will be no way to write the
CloseHandle function.
So considering that they share the same CloseHandle, that will be
impossible.
If CreateFile returns 0 as valid handle, CloseHandle has no way
to tell if it's an invalid CreateFileMapping handle, or a valid
CreateFile handle.
Both CreateFile and CreateFileMapping return kernel handles, which
should be basically indexes into the same kernel object table.
On NT/W2K, GDI handle has a quite robust design. Each handle is made up
of a type identifier byte, recycle-count byte, and an index (word) to a
system-wide table. So it's very easy to verify a handle.
Feng Yuan
You seem to be looking for some function aside from CreateFile which
returns a file handle that would be NULL instead of
INVALID_HANDLE_VALUE. You can throw out some of the obvious
HANDLE-returning functions simply because they make no sense. For
example CreateMutex returns NULL if it fails but I can't imagine
anyone trying to create file mapping on a mutex handle. In fact I'm
not aware of anyone creating file mappings on anything other than disk
files, though I don't doubt it's possible.
If you are concerned about this and you always want CreateFileMapping
to succeed, why not look for NULL handles when making your call to
CreateFileMapping and automatically change them to
INVALID_HANDLE_VALUE? Like this?
HANDLE CreateMyMapping(HANDLE hFile, ...)
{
HANDLE hActual = NULL == hFile ? INVALID_HANDLE_VALUE : hFile;
return CreateFileMapping(hActual, ...);
}
Joe O'
"Marco Dalla Gasperina" <mar...@home.com> wrote in message
news:Bw4b5.9873$V34.1...@news1.sttls1.wa.home.com...
> Hi there,
>
> Some Win32 API that returns a handles specify that they
> return INVALID_HANDLE_VALUE when they fail (e.g. CreateFile).
> Some of them say they return NULL (e.g. CreateFileMapping).
>
> In all the cases I'm interested in I free the handle with
> CloseHandle(). Are there any cases where CreateFile()
> can return a valid NULL handle and CreateFileMapping()
> can return INVALID_HANDLE_VALUE and succeed?
>
> thanks,
> marco
>
>
>
I've long considered adding something like a handle_traits<> template
to distinguish say mutex handles from file handles I hate doing that,
though.
Joe
"Marco Dalla Gasperina" <mar...@home.com> wrote
> "David Lowndes" <dav...@mvps.org> wrote in message