8

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?

Afriza N. Arief
  • 7,696
  • 5
  • 47
  • 74
Aussay Marshal
  • 157
  • 2
  • 10
  • I didn't understand any thing can you explain what is your question ??? – Qchmqs Aug 14 '11 at 08:49
  • Thank you for your post, it's oke if you don't understand, please ignore and read what people post if any brings you any interesting ideas. I am sure you will get hooked up soon :-D – Aussay Marshal Aug 14 '11 at 08:54
  • 4
    @Serious: *"It's oke if you don't understand"* ?! No, it's not: If you post a *good* question on Stack Overflow, that adds value to a great programming resource. If you post a bad or incomprehensible question, on Stack Overflow, that will actually *hurt* Stack Overflow's usefulness (even if just a little). So please, make an effort to phrase your questions as well as you can, for the sake of the community! (I've edited your question a bit, btw.) – stakx - no longer contributing Aug 14 '11 at 09:10

5 Answers5

7

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • Thanks, is there a mechanism to deliver handle to open window ? What is that if you could share ? – Aussay Marshal Aug 14 '11 at 08:51
  • I would like to know if there is a way to deliver handle value to each open windows ? I have no INTENTION to get the gone window's handle. My knowledge about windows is not good, I make questions. – Aussay Marshal Aug 14 '11 at 09:03
  • No - no GUI handle has a reference counter. So there is just one handle value and you can't do something like "DuplicateHandle" Its a design bug in the same way as UNIX uses process id values. – Lothar May 26 '15 at 09:52
4

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.

RbMm
  • 31,280
  • 3
  • 35
  • 56
3

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.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
  • My answer is based on [the comment](http://stackoverflow.com/questions/7055869/are-window-handles-hwnd-unique-or-do-they-ever-get-reused/32950786#comment8439790_7055947) by @CodyGray. I just wanted to make this fact more prominent. – Alexey Ivanov Oct 05 '15 at 14:18
2

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.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Thank ybungalabill, that's exactly what I would like to know.Thumbup! – Aussay Marshal Aug 14 '11 at 09:04
  • 2
    Only some of this answer is true. Window handles do *not* retain compatibility with 16-bit Windows. They're pointers, which means that their size is architecture-dependent. On 64-bit versions of Windows, they're 64-bit values. On 32-bit versions of Windows, they're 32-bit values. Check the Windows header files for details. – Cody Gray - on strike Aug 14 '11 at 14:34
  • 1
    The `HANDLE` type is not a typedef for `void*` anymore; it's been *years* since that was the case. Parts of the MSDN documentation haven't been updated since then apparently, but that doesn't make the assumption any less incorrect. Moreover, that's not even what the article you linked to says. It only says that there is a limit of 65,536 user handles per session. That doesn't say anything about the data type used to store the handles. You're drawing conclusions that aren't explicitly stated and are quite unjustified. – Cody Gray - on strike Aug 14 '11 at 15:39
  • 1
    If you're still using the headers that came with VC 6, you're hopelessly out of date. I'm not sure what else to tell you. It's not defined that way in any modern version of the SDK. I don't really understand what you want from MSDN. You want a link that says MSDN is wrong from MSDN? Sorry, that isn't going to happen. Not every page in the online documentation gets updated regularly, and even when updates *do* happen, stuff gets missed. Raymond Chen writes an entire blog about that. You provided a link to an irrelevant article about GDI handles, which has nothing to do with `HWND` values. – Cody Gray - on strike Aug 14 '11 at 15:54
  • 1
    And none of that really has anything to do with anything. We aren't talking about GDI tables. The point is that window handles are not backwards-compatible with 16-bit versions of Windows. If you declare the `HWND` type as a `short int`, things aren't going to work properly. It's not a 16-bit value. – Cody Gray - on strike Aug 14 '11 at 15:55
  • 1
    Hmm, remember that `STRICT` is defined by default now, and has been for years. You must be looking at the wrong branch of the `#if` statement. And that article is talking about GDI handles, not window handles. You have to read the whole thing, not just the part of it that suits your argument. I'm not arguing with the fact that the values will be re-used. I'm contesting the specious claim that window handles retain compatibility with 16-bit Windows. – Cody Gray - on strike Aug 14 '11 at 16:15
  • 1
    @Cody: excuse me, but I'm not going to argue with you further if you don't do even a minimal effort to go and check that HANDLE is typedefed to a void pointer *when STRICT is defined* and to an integer otherwise. Unless you do a minimal effort to actually look at the table in the article I linked to where it's written that windows are user objects, and the article is talking about user objects (you just made up that it's "talking about GDI handles"). – Yakov Galka Aug 14 '11 at 16:32
  • 5
    Remember, we're not talking about `HANDLE`. We're talking about `HWND`. I don't understand why you keep talking about things that are completely irrelevant, like GDI handles and the `HANDLE` type. The question title very clearly says "window handles". I'm not making this up. And I've already addressed that the table is quite irrelevant to this discussion. And finally, we get to the real heart of your argument, which is completely and totally incorrect. Win16 applications *will not work* on 64-bit versions of Windows. End of story. – Cody Gray - on strike Aug 14 '11 at 16:40
  • 3
    It is mentioned in [MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751.aspx): `typedef HANDLE HWND;`, `typedef PVOID HANDLE;`, `typedef void *PVOID;`. And the `User Objects` [article](http://msdn.microsoft.com/en-us/library/ms725486.aspx) mentions `HWND`, not GDI Objects. `GDI Objects` have separate [article](http://msdn.microsoft.com/en-us/library/ms724291.aspx) listed in the navigation. And lastly, a bit reference to the [handle table](http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx). – Afriza N. Arief Oct 23 '13 at 06:51
  • @YakovGalka the type of `HANDLE` is `PVOID`, indeed a 64 bit void pointer. This is generic, it can be a 64 bit address or it can be an index into a handle table, or it can be a pseudohandle, depending on the handle – Lewis Kelsey Mar 19 '21 at 05:58
1

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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Thank you, your post and his (ybungabol) are very helpful. He posted first so I vote his as accepted answer. My reputation is only 12 at present, I'll upvote you next time if we have a chance to meet again. – Aussay Marshal Aug 14 '11 at 09:22
  • @Serious, instead of saying "thumbup" (as with ybungalobill's answer) or "very helpful" (with my answer), just press the up-arrow on the left side of a question! – stakx - no longer contributing Aug 14 '11 at 09:25
  • 3
    You *definitely* should assume that handle values can be reused. It's not a "potentially" thing. The documentation for the [`IsWindow()` function](http://msdn.microsoft.com/en-us/library/ms633528.aspx) makes this explicit: *"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."* – Cody Gray - on strike Aug 14 '11 at 14:39
  • @Cody, you're right, I may perhaps have been too prudent. ;-) I've updated my answer. – stakx - no longer contributing Aug 14 '11 at 15:36
  • 2
    The question is not about representation of a handle, it's about lifetime of thge handle. – Lothar May 26 '15 at 09:55