I am thinking if there are handles of the same value ?
To clarify my question, let's say I open Notepad, type in some text, save it and then close Notepad. If I repeat this a thousand times (or even more), will I ever have a chance to see the same window handle (HWND) value being used for the Notepad main window that was used the first time? If so, why?
By the pigeonhole principal, yes, they can't be unique. In fact, Microsoft still maintains compatibility with 16-bit Windows, and as a result handles are 16-bit values. So there are at most 65536 possible handle values.
Yes. There are only a finite number of values a handle can be represented by, so Windows has to reuse them eventually.
Once a handle is closed, it is gone, you can't do anything with it, it doesn't exist, and you shouldn't even look at it.
And if you subsequently open another handle, then it is possible that Windows will reuse the handle value.
I would advise you to make absolutely no assumptions about handle values.
You shouldn't have to think about concrete handle values for all practical purposes. A handle should be considered an opaque placeholder for something else. You can pass the handle around to refer to something (e.g. a window) without having a reference to the real thing, but you shouldn't ever have to look at the handle itself. The fact that it is a numeric value should be considered an implementation detail, ie. not important (unless maybe you do some kind of low-level systems programming).
That being said, I'd support @jalf's answer: Handle values could get reused. If I had to make any assumption at all about that, I would assume that a handle value could get reused anytime.
Yes, window handles are reused.
Documentation to IsWindow
function says:
A thread should not use
IsWindow
for a window that it did not create because the window could be destroyed after this function was called. Further, because window handles are recycled the handle could even point to a different window.
来源:https://stackoverflow.com/questions/7055869/are-window-handles-hwnd-unique-or-do-they-ever-get-reused