Bad news. For historical reasons, the Windows API usually does not have a valid "invalid handle" value. Various subsystems on Windows consider either NULL or INVALID_HANDLE_VALUE as an invalid descriptor value (both for returning an invalid descriptor value and for accepting it). Related article about Old New Thing .
However, the good news is that although you still need to be prepared for unexpected return values ββ(unless you dig up the documentation for each function you use), providing either an invalid value works "in practice."
You may not have used the correct designated βinvalidβ value, but it is still invalid and therefore the function will work. Your application will not crash, there are no negative consequences, except for wasted several processor cycles.
So go nullptr and use NULL (or nullptr ), this is good for you. If nothing more, this is intuitive for someone reading your code later. In your specific example, this is also correct, since GDI functions accept NULL as invalid.
You can rely heavily on invalid NULL and INVALID_HANDLE_VALUE values. Although I am not aware of the requirement (as indicated in an explicit statement in the documentation) that a valid HANDLE must be nonzero, in practice they always exist. I bet you won't find a handle with a null value (just try using the Sysinternals HANDLE tool, no process on your computer will probably have a handle below 20).
But even if you assume that NULL may be a valid descriptor value, you must bear in mind that some descriptors are already open and closed before the main call, or global constructors are started, you really have no choice, This means that assuming that NULL may be a valid descriptor, and assuming that it is still in effect at the time your program starts, the likelihood that this hypothetical descriptor accidentally is of a type compatible with the API function is very low.
On the other hand, it can be argued that an application can have (unsigned) -1 handle open, rendering INVALID_HANDLE_VALUE valid value.
If you donβt miss the pens, I canβt imagine how you will ever get a lot of open pens. But more importantly, long before you press this number, you will probably run out of memory on a 64-bit system, and you will end up address space on a 32-bit system.
If INVALID_HANDLE_VALUE , which is a valid handle, ever becomes a problem, you are having a much more serious problem.