Testing for an invalid windows handle: should I compare with 'NULL', '0' or even 'nullptr'? Testing for an invalid windows handle: should I compare with 'NULL', '0' or even 'nullptr'? windows windows

Testing for an invalid windows handle: should I compare with 'NULL', '0' or even 'nullptr'?


Compare it against the documented error return value. That means that you should compare it against INVALID_HANDLE, 0, -1, non-zero, or <=32 (I'm not kidding with the last one, see ShellExecute).


To answer your question: the HANDLE type is declared in winnt.h as

typedef PVOID HANDLE;

Hence, technically it is a pointer.

However, I would just use whatever is documented; if the documentation states that NULL is returned, I use exactly that unless evidence shows that the documentation is incorrect.

I don't even think about pointers vs. integers. NULL is just an opaque value (in this situation) and HANDLE is an opaque type to me and I don't bother looking up what it is #define'd to.


I think INVALID_HANDLE_VALUE is usually the proper 'invalid' value for windows handles...and that evaluates to -1.