Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 218
Aussay Marshal Avatar asked Aug 14 '11 08:08

Aussay Marshal


People also ask

How do I find my windows handle?

However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow() .

What do you call the handle on a window?

The two main types of window handles that are fitted onto double glazed windows are, Espag handles and Cockspur handles. Then you have tilt and turn handles, spade or blade handles and monkey tail handles. Below, we'll look at these different types of window handles, how they work and why they're used.

How does Hwnd work?

HWND is a special HANDLE which points to a window object. HWND is said to be a pointer to a Window. To get any Window, its Child or Dialog box object, we need to use an HWND object. Communication between two windows is also done using HWND's.


4 Answers

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.

like image 68
jalf Avatar answered Nov 09 '22 16:11

jalf


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.

like image 23
RbMm Avatar answered Nov 09 '22 15:11

RbMm


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.

like image 38
Yakov Galka Avatar answered Nov 09 '22 14:11

Yakov Galka


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.

like image 2
Alexey Ivanov Avatar answered Nov 09 '22 16:11

Alexey Ivanov