Re: INVALID_HANDLE_VALUE vs. NULL
- From: "Slava M. Usov" <stripit.slough@xxxxxxx>
- Date: Tue, 17 May 2005 19:50:18 +0200
"Gary Chanson" <gchanson@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:enQrDzMWFHA.376@xxxxxxxxxxxxxxxxxxxxxxx
[...]
> 0 is a valid handle and passing it to CloseHandle does not cause an
> exception.
On any NT-derived platform, 0 (zero) can never be a valid handle value. A
zero handle will either result in an error or special behavior. For example,
the native ZwCreateProcess() service has a section handle argument, which is
always supplied by win32's CreateProcess(). If that handle is zero,
ZwCreateProcess() will use the fork() semantics for process creation [which
is to copy the address space of the parent]. For another example,
ZwTerminateProcess() takes -1, 0 or a valid process handle; and the zero
handle will _not_ terminate the process, unlike the other two cases, but
again do something very special. This can be repeated for just about any
native OS routine that accepts handles; comparison against zero is
ubiquitous in the OS code, so if any valid handle somehow had such a value,
you would not be able to perform any "regular" operation on this handle.
The INVALID_HANDLE_VALUE, which is -1, is in fact a valid handle, which
means "this process". Of course, ReadFile() will fail with this handle,
because the object type is wrong, just like ReadFile() will fail on a
perfectly valid handle of a mutex, a thread, etc.
CloseHandle() does indeed fail when given a bad handle. It will fail on
both -1 and 0: -1 cannot be closed, and 0 is just wrong. When CloseHandle()
fails, it checks whether a debugger is attached to the process, in which
case it raises an exception.
Now, somebody mentioned Windows CE and sockets. I do not know whether
Windows CE is officially recognized as a win32 platform, but I would still
like to point out that Windows CE is not NT-based and sockets do not have to
be handles.
S
.
- Follow-Ups:
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Arkady Frenkel
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Gary Chanson
- Re: INVALID_HANDLE_VALUE vs. NULL
- References:
- INVALID_HANDLE_VALUE vs. NULL
- From: Frank A. Uepping
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Arkady Frenkel
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Frank A. Uepping
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Gary Chanson
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: qfel
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Gary Chanson
- INVALID_HANDLE_VALUE vs. NULL
- Prev by Date: Re: How to get the PhysicalDrive number which the logical drive locate
- Next by Date: Re: INVALID_HANDLE_VALUE vs. NULL
- Previous by thread: Re: INVALID_HANDLE_VALUE vs. NULL
- Next by thread: Re: INVALID_HANDLE_VALUE vs. NULL
- Index(es):
Relevant Pages
|