Re: INVALID_HANDLE_VALUE vs. NULL
- From: "Gary Chanson" <gchanson@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 17 May 2005 14:20:32 -0400
"Slava M. Usov" <stripit.slough@xxxxxxx> wrote in message
news:%23Sl3FkwWFHA.2128@xxxxxxxxxxxxxxxxxxxxxxx
> "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.
Then why did they make INVALID_HANDLE_VALUE -1?
> 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.
When I try it, I don't see an exception for either -1 or 0.
--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.mvps.org/ArcaneIncantations/consulting.htm
-gchanson@xxxxxxxx
.
- Follow-Ups:
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Mark Henry
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Slava M. Usov
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Pete Gontier
- 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
- Re: INVALID_HANDLE_VALUE vs. NULL
- From: Slava M. Usov
- INVALID_HANDLE_VALUE vs. NULL
- Prev by Date: Re: INVALID_HANDLE_VALUE vs. NULL
- Next by Date: Re: How to get the PhysicalDrive number which the logical drive locate
- Previous by thread: Re: INVALID_HANDLE_VALUE vs. NULL
- Next by thread: Re: INVALID_HANDLE_VALUE vs. NULL
- Index(es):
Relevant Pages
|