{"id":33883,"date":"2005-10-06T10:00:09","date_gmt":"2005-10-06T10:00:09","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/10\/06\/the-unfortunate-interaction-between-load_library_as_datafile-and-dialogbox\/"},"modified":"2005-10-06T10:00:09","modified_gmt":"2005-10-06T10:00:09","slug":"the-unfortunate-interaction-between-load_library_as_datafile-and-dialogbox","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20051006-09\/?p=33883","title":{"rendered":"The unfortunate interaction between LOAD_LIBRARY_AS_DATAFILE and DialogBox"},"content":{"rendered":"<p>Some people have noticed that if you load a DLL with the <code>LOAD_LIBRARY_AS_DATAFILE<\/code> flag, you sometimes get strange behavior if you then pass that <code>HINSTANCE<\/code> to a dialog box function.<\/p>\n<p> The problem here is that since the bottom 16 bits of a proper <code>HINSTANCE<\/code> are always zero, different components have &#8220;borrowed&#8221; those bits for different purposes. The kernel uses the bottom bit to distinguish modules loaded by having been mapped into memory as sections (i.e., loaded normally) from those who have been mapped as one giant block (loaded as a datafile). It needs to know this so that the various resource-management functions such as <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/resources\/introductiontoresources\/resourcereference\/resourcefunctions\/findresource.asp\"> the <code>FindResource<\/code> function<\/a> know how to interpret the data in order to locate the resource in question. Although everybody now knows that the <code>HINSTANCE<\/code> is the base address of the DLL, in principle, it is an opaque value (and in the 16-bit world, the value was indeed opaque). <\/p>\n<p> Meanwhile, the window manager has its own problems. In order to support 16-bit applications running seamlessly on the desktop (<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/10\/05\/477317.aspx\">not in a virtual machine, as discussed earlier<\/a>), as well as thunking between 16-bit and 32-bit code, it needs to accept both 32-bit <code>HINSTANCE<\/code> values as well as 16-bit <code>HINSTANCE<\/code> values. Since <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/10\/08\/55239.aspx\"> memory allocation granularity is 64KB<\/a>, the window manager knows that valid 32-bit <code>HINSTANCE<\/code>s have zero in the bottom 16 bits, whereas 16-bit <code>HINSTANCE<\/code> values are nonzero there. <\/p>\n<p> Perhaps you see the conflict now. <\/p>\n<p> If you pass the instance handle of a DLL loaded as a datafile, the kernel will set the bottom bit as a signal to itself to locate its resources in the flat datafile manner rather than in the mapped DLL manner. But the window manager sees that the bottom 16 bits are not all zero and assumes that it has been given a 16-bit <code>HINSTANCE<\/code> value. <\/p>\n<p> Amazingly, this doesn&#8217;t cause a problem most of the time because the things that need to be handled differently between 32-bit and 16-bit <code>HINSTANCE<\/code>s are relatively minor. The one that is most likely to bite you is the dialog instance data segment. <\/p>\n<p> In 16-bit Windows, a dialog box came with its own data segment, which was used as the local data segment for controls hosted by that dialog box. Most controls didn&#8217;t need a lot of storage in the local data segment, so the issue of where it came from wasn&#8217;t really important. The big exception was edit controls, since they can contain multiple kilobytes of text. a dozen kilobytes of text may very well not fit in the application&#8217;s data segment. Therefore, creating a new data segment gave the edit controls on the dialog a new 64KB block of memory to store their data in. Programs were expected to extract the data from the edit control via mechanisms such as <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/08\/21\/54675.aspx\"> the <code>GetWindowText<\/code> function<\/a> and store the result someplace that had the capacity to handle it (outside the cramped local data segment). <\/p>\n<p> In order to maintain compatibility with 16-bit programs who are expecting this behavior to continue, the window manager, when it sees a 16-bit <code>HINSTANCE<\/code>, dutifully creates a 16-bit data segment in which to store the data for the edit controls, using a helper function provided by the 16-bit emulation layer. But if you aren&#8217;t really a 16-bit program, then the 16-bit emulation layer is not active, and consequently  it never got a chance to tell the window manager how to create one of these compatibility segments. Result: Crash. <\/p>\n<p> The solution is to add the <code>DS_LOCALEDIT<\/code> style to your dialog box styles. This flag means &#8220;Do not create a dialog box data segment; just keep using the data segment of the caller.&#8221; Therefore, when your <code>LOAD_LIBRARY_AS_DATAFILE<\/code> <code>HINSTANCE<\/code> is mistaken for a 16-bit dialog template, the dialog manager won&#8217;t try to create a dialog box data segment and therefore won&#8217;t call that function that doesn&#8217;t exist. <\/p>\n<p> I believe this issue has been resolved in Windows XP&nbsp;SP&nbsp;2. The window manager uses a different mechanism to detect that it is being asked to create a dialog box on behalf of a 16-bit program and is no longer faked out by the faux-<code>HINSTANCE<\/code>s produced by <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/dllproc\/base\/loadlibraryex.asp\"> the <code>LoadLibraryEx<\/code> function<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some people have noticed that if you load a DLL with the LOAD_LIBRARY_AS_DATAFILE flag, you sometimes get strange behavior if you then pass that HINSTANCE to a dialog box function. The problem here is that since the bottom 16 bits of a proper HINSTANCE are always zero, different components have &#8220;borrowed&#8221; those bits for different [&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":[25,2],"class_list":["post-33883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-history"],"acf":[],"blog_post_summary":"<p>Some people have noticed that if you load a DLL with the LOAD_LIBRARY_AS_DATAFILE flag, you sometimes get strange behavior if you then pass that HINSTANCE to a dialog box function. The problem here is that since the bottom 16 bits of a proper HINSTANCE are always zero, different components have &#8220;borrowed&#8221; those bits for different [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/33883","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=33883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/33883\/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=33883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=33883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=33883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}