{"id":25983,"date":"2007-07-17T10:00:00","date_gmt":"2007-07-17T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/07\/17\/how-are-window-manager-handles-determined-in-windows-nt\/"},"modified":"2007-07-17T10:00:00","modified_gmt":"2007-07-17T10:00:00","slug":"how-are-window-manager-handles-determined-in-windows-nt","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070717-00\/?p=25983","title":{"rendered":"How are window manager handles determined in Windows NT?"},"content":{"rendered":"<p>We left off our story last time by raising the problem of programs that send messages to windows that have already been destroyed and how window handle re-use exacerbates the problem. Although this is clearly a bug in the programs that use window handles after destroying the window, the problem is so widespread that the window manager folks in Windows&nbsp;NT decided to take a more proactive approach.\n (Reiterating in case you couldn&#8217;t figure it out: This entry goes &#8220;behind the scenes&#8221;. Behavior described here falls into the category of &#8220;implementation detail&#8221; and is subject to change at any time.)\n In Windows&nbsp;NT, the 32-bit <code>HWND<\/code> was broken into two parts. The bottom 16 bits acted like the window handle of Windows&nbsp;95: It was an index into a table. But whereas Windows&nbsp;95 left the upper 16 bits zero for compatibility with 16-bit programs, Windows&nbsp;NT used the top bits as a &#8220;uniquifier&#8221;.\n For example, the first time entry 0x0124 in the table was used, the handle corresponding to that entry was 0x00010124. After that window is destroyed and a new one created in slot 0x0124, the handle for the new entry is 0x000<u>2<\/u>0124. Each time the entry is re-used, the uniquifier increments by one.\n But what about those 16-bit programs?\n When a 16-bit program used a window handle, the window manager would just use that value as the index into the table. There was no uniquifier, so the window manager just ran with whatever was in that slot. After all, the uniquifier is wasn&#8217;t essential to correct operation of the window manager; it was added merely to cover for buggy programs. Consequently, 16-bit programs were just as susceptible to the &#8220;handle re-use problem&#8221; as they were in Windows&nbsp;3.1 and Windows&nbsp;95. Only 32-bit programs got the extra protection.\n But since the window handle was an index, the full 16-bit range of window handles became available (minus special values like <code>NULL<\/code>), for a theoretical maximum of around 65,000 windows. Well, except that the actual theoretical maximum is is half that, around 32,700 windows. Why did we lose half of the window handles? Because there are programs out there that assume that window handles are always even numbers!<\/p>\n<p> Next time, we&#8217;ll look at the magical 10,000 per-process handle limit. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We left off our story last time by raising the problem of programs that send messages to windows that have already been destroyed and how window handle re-use exacerbates the problem. Although this is clearly a bug in the programs that use window handles after destroying the window, the problem is so widespread that the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-25983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>We left off our story last time by raising the problem of programs that send messages to windows that have already been destroyed and how window handle re-use exacerbates the problem. Although this is clearly a bug in the programs that use window handles after destroying the window, the problem is so widespread that the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/25983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=25983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/25983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=25983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=25983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=25983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}