Are window handles (HWNDs) unique or are they ever reused?

I think if there are descriptors of the same value?

To clarify my question, let's say I open Notepad, type in text, save it and close Notepad. If I repeat this a thousand times (or even more), will I ever have the opportunity to see the value of the handle window (HWND) that is used in the main Notepad window that was used for the first time? If so, why?

+6
source share
4 answers

Under the main character , yes, they cannot be unique. In fact, Microsoft still supports 16-bit Windows compatibility, and the resulting handles are 16-bit values. Thus, there are no more than 65536 possible descriptor values.

+2
source

Yes. There is only a finite number of values ​​that can be represented by a descriptor, so Windows should reuse them.

As soon as the pen is closed, it has disappeared, you cannot do anything with it, it does not exist, and you should not even watch it.

And if you later open another descriptor, it is possible that Windows will reuse the descriptor value.

+6
source

I would advise you to make absolutely no assumptions about the descriptor values.

You do not need to think about specific descriptor values ​​for all practical purposes. A pen should be considered an opaque placeholder for something else. You can pass a descriptor to refer to something (for example, a window) without having a link to the real thing, but you will never have to look at the descriptor itself. The fact that this is a numerical value should be considered as a detail of implementation, i.e. it doesn’t matter (if, perhaps, you are not performing some low-level system programming).

As they say, I would support @jalf's answer: Handle values ​​could be reused. If I had to make any assumptions about this, I would suggest that the descriptor value can be reused at any time.

+2
source

Yes, window handles are reused.

The documentation for the IsWindow function says:

A thread should not use IsWindow for a window that it did not create, because the window could be destroyed after calling this function. In addition, as window handlers return, the handle may even point to another window.

0
source

Source: https://habr.com/ru/post/895003/


All Articles