Re: INVALID_HANDLE_VALUE vs. NULL

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 5/17/05 11:20 AM, in article #62X22wWFHA.3676@xxxxxxxxxxxxxxxxxxxx, "Gary
Chanson" <gchanson@xxxxxxxxxxxxxxxxxxxx> wrote:

>> 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?

I believe this page has been linked at least twice so far in this thread,
but another link can't hurt (especially since this is not the only reason I
am posting yet again):

http://blogs.msdn.com/oldnewthing/archive/2004/03/02/82639.aspx

>> 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.

I haven't been able to make the exception happen either. I may not be
running an appropriate flavor of debugger. However, CloseHandle does fail
when passed 0 and a subsequent call to GetLastError does return
ERROR_INVALID_HANDLE.

.



Relevant Pages

  • 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)
  • Re: IllegalArgumentException vs. NullPointerException and collection behavior
    ... There is an advantage to declaring a checked exception - it forces client programmers to pay attention to it. ... The trouble with throwing any RuntimeException is that the API clients might fail abruptly because their programmers didn't pay close enough attention to the Javadocs. ... Throw a checked Exception if the client should have been able to count on getting a result, and you want to force awareness of any out-of-band problems. ...
    (comp.lang.java.help)
  • 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: Try..Except fails. Also - any other ways?
    ... case - you see, the file i'm testing on - a log file, is getting logged ... it fail, or if it does fail - for this to be handled. ... Btw - Any ideas why the catch-all exception handling didn't work either ... You will get a 'bad' Handle -1 if FileOpen fails ...
    (comp.lang.pascal.delphi.misc)
  • Re: trouble passing float array from C to java method
    ... running in the background and calling my Java function when it gathers ... After every call to JNI you should check for errors (there are, in fact, a few ... This can fail. ... This can fail by throwing a Java exception. ...
    (comp.lang.java.programmer)