Are window handles (HWND) unique, or do they ever get reused? Are window handles (HWND) unique, or do they ever get reused? windows windows

Are window handles (HWND) unique, or do they ever get reused?


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.


theoretically yes. in practice - the probability of this (in contrast to process and thread id, which is frequently reused) is almost zero.

in current implementation low 16 bits of HWND used as index in windows handle table - so currently maximum 64K windows can be created. the next 16 bits used as reuse index. when a cell is used for the first time this index is 1.when this cell is reused, the index is increased by 1. and so on. as result for get the same HWND on window need how minimum 64k windows must be created and destroyed. but this is only in case all this windows will be used the same cell. but we have 64k cells. so real minimum much more higher for this. not exactly 2^32 but big enough.

and even if implementation will changed, i not think that new implementation will make HWND less unique than current.


By the pigeonhole principal, yes, they can't be unique.

Due to the compatibility with 32-bit processes (WoW64), handles cannot use the entire 64-bits even on 64-bit OS -- think of a 64-bit process passing a handle to a 32-bit child, or getting a handle to a window opened by a 32-bit process. This makes their true space pretty small, and thus reuse very likely.