CreateEvent , INVALID_HANDLE_VALUE and WaitForSingleObject RRS feed

  • Question

  • I was reading up on this blog:



    herein, Raymond Chen makes this comment towards the end:


    Fourth, you have to be particularly careful with the INVALID_HANDLE_VALUE value: By coincidence, the value INVALID_HANDLE_VALUE happens to be numerically equal to the pseudohandle returned by GetCurrentProcess(). Many kernel functions accept pseudohandles, so if if you mess up and accidentally call, say, WaitForSingleObject on a failed INVALID_HANDLE_VALUE handle, you will actually end up waiting on your own process. This wait will, of course, never complete, because a process is signalled when it exits, so you ended up waiting for yourself.


    Now, CreateEvent, per documentation could infact return a value = INVALID_HANDLE_VALUE when successful. So, passing this to WaitForSingleObject , which is most natural way to use an event object, seems to be frought with danger. Does Windows take care of not Creating an event object with a handle value equal to INVALID_HANDLE_VALUE ?

    Monday, October 1, 2007 10:05 PM