Re: INVALID_HANDLE_VALUE vs. NULL




"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

.



Relevant Pages

  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... > On any NT-derived platform, 0 (zero) can never be a valid handle value. ... Of course, ReadFilewill fail with this handle, ... > CloseHandle() does indeed fail when given a bad handle. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... On any NT-derived platform, 0 (zero) can never be a valid handle value. ... Of course, ReadFilewill fail with this handle, ... case it raises an exception. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... zero handle will either result in an error or special behavior. ... If that handle is zero, ZwCreateProcesswill use the forksemantics for process creation. ... Of course, ReadFilewill fail with this handle, because the object type is wrong, just like ReadFilewill fail on a perfectly valid handle of a mutex, a thread, etc. ... fails, it checks whether a debugger is attached to the process, in which case it raises an exception. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... >> CloseHandle() does indeed fail when given a bad handle. ... I don't see an exception for either -1 or 0. ... running an appropriate flavor of debugger. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Benefits of Dynamic Typing
    ... The latter problem is detected in parts not specific to the compiler, ... and the only information about the reason is carried by the exception ... the entire Map call fails with the same exception. ... but it did expect that some application might fail. ...
    (comp.lang.functional)