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

馋奶兔 提交于 2019-12-07 02:56:41

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!