Re: INVALID_HANDLE_VALUE vs. NULL

Tech-Archive recommends: Fix windows errors by optimizing your registry



"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


.



Relevant Pages

  • Re: File IO-I am defeated!
    ... So add some code to trap the error and have the error routine run the code I posted in my last response, except change my code so that it send its output to a log file instead of to a ListBox. ... You are telling us that these files contain only "standard text characters", and that you have produced them using your own code so you are certain you are correct. ... Your own code will happily load a file containing whatever bytes values you wish, whether they are what you would call standard text characters or not, EXCEPT it will fail with exactly the error you describe if there is a Chror a Chranywhere in the file. ... I would bet my bottom dollar that when you eventually get the log back from the faulty file you will find at least one of those characters in there, most probably the zero. ...
    (microsoft.public.vb.general.discussion)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... 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. ... > case it raises an exception. ...
    (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, ... > CloseHandle() does indeed fail when given a bad handle. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: quality control
    ... Almost always, zero. ... will be, on the average, only 1 bad item in 100 samplings -- ... You expect 1 failure in 10,000. ... I'm thinking with the binary outcome of fail/not fail, ...
    (sci.stat.math)
  • 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)