窗口句柄 (HWND) 是唯一的,还是会被重用?
我在想是否有相同价值的手柄?
为了澄清我的问题,假设我打开记事本,输入一些文本,保存它,然后关闭记事本。如果我重复此操作一千次(甚至更多),我是否有机会看到第一次使用的记事本主窗口使用相同的窗口句柄(HWND)值?如果是这样,为什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。句柄只能表示有限数量的值,因此 Windows 最终必须重用它们。
一旦句柄关闭,它就消失了,你不能用它做任何事情,它不存在,你甚至不应该看它。
如果您随后打开另一个句柄,那么 Windows 可能会重用该句柄值。
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.
理论上是的。实际上,这种情况的概率(与经常重用的进程和线程 ID 相比)几乎为零。
在当前实现中,
HWND
的低 16 位用作窗口句柄表中的索引 - 因此当前最多可以创建 64K 窗口。接下来的 16 位用作重用索引。当一个小区第一次被使用时,该索引为1。当该小区被重新使用时,该索引增加1。以此类推。作为在窗口上获得相同HWND
的结果,需要如何创建和销毁最少 64k 的窗口。但这只是在所有这些窗口都将使用同一个单元格的情况下。但我们有 64k 个单元。所以实际的最低值要高得多。不完全是 2^32 但足够大。即使实现发生变化,我认为新的实现不会使
HWND
不如当前的唯一。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 sameHWND
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.是的,窗口句柄被重复使用。
IsWindow< 的文档/code> 函数
说:
Yes, window handles are reused.
Documentation to
IsWindow
function says:根据 pigeonhole 主体,是的,它们不能是唯一的。
由于与 32 位进程 (WoW64) 的兼容性,即使在 64 位操作系统上,句柄也无法使用整个 64 位 - 想象一下 64 位进程将句柄传递给 32 位子进程,或获取句柄到由 32 位进程打开的窗口。这使得它们的真实空间非常小,因此很可能被重复使用。
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.
我建议您绝对不要对句柄值做出任何假设。
您不必为了所有实际目的而考虑具体的句柄值。句柄应被视为其他内容的不透明占位符。您可以传递句柄来引用某些东西(例如窗口),而无需引用真实的东西,但是您不必查看句柄本身。它是一个数值这一事实应该被视为一个实现细节,即。并不重要(除非您可能进行某种低级系统编程)。
话虽这么说,我支持@jalf 的答案:句柄值可以被重用。如果我必须对此做出任何假设,我会假设句柄值可以随时重用。
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.
确认这个问题答案的最简单方法之一是编写一个程序并实际测试它。
我写了一个程序,不断创建和销毁窗口,记录使用过的HWND值,最后输出获取重复HWND值所需的次数。
结论:
创建和销毁65534个窗口后,您将有机会获得重复的HWND值
以下是我的测试代码。我将程序编译为32位,因为在64位中,出于未知原因,我有时会得到HWND,其中高位双字不是zreo(例如0xffffffff80230918、0xffffffff80000e68、0xffffffff802d0918)
One of the easiest ways to confirm the answer to this question is to write a program and actually test it.
I wrote a program that continuously creates and destroys windows, records the used HWND values, and finally outputs the number of times it takes to obtain repeated HWND values.
In conclusion:
After 65534 windows are created and destroyed, you will have a chance to get duplicate HWND values
The following is my test code. I compiled my program to 32-bit because in 64-bit, for unknown reasons, I sometimes get HWND with high-order dword not being zreo (such as 0xffffffff80230918, 0xffffffff80000e68, 0xffffffff802d0918)