{"id":5833,"date":"2012-12-14T07:00:00","date_gmt":"2012-12-14T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/12\/14\/why-is-it-possible-to-create-a-toolbar-with-the-wrong-hinstance-and-whats-the-right-hinstance-anyway\/"},"modified":"2012-12-14T07:00:00","modified_gmt":"2012-12-14T07:00:00","slug":"why-is-it-possible-to-create-a-toolbar-with-the-wrong-hinstance-and-whats-the-right-hinstance-anyway","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121214-00\/?p=5833","title":{"rendered":"Why is it possible to create a toolbar with the wrong HINSTANCE? And what&#039;s the right HINSTANCE anyway?"},"content":{"rendered":"<p>\nA customer observed that all of the following code fragments\nare successful in creating a toolbar common control:\n<\/p>\n<pre>\n\/\/ <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh298381.aspx\">Fragment 1<\/a>: Use the process instance\n    \/\/ Create the toolbar.\n    HWND hWndToolbar = CreateWindowEx(\n        0, TOOLBARCLASSNAME, NULL,\n        WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,\n        hWndParent, NULL, g_hInst, NULL);\n<\/pre>\n<pre>\n\/\/ <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh298391.aspx\">Fragment 2<\/a>: Use the comctl32 instance\n    \/\/ Create the toolbar.\n    HWND hWndToolbar = CreateWindowEx(\n        0, TOOLBARCLASSNAME, L\"Toolbar\",\n        WS_CHILD | WS_VISIBLE | WS_BORDER,\n        0, 0, 0, 0,\n        hWndParent, NULL, HINST_COMMCTRL, NULL);\n<\/pre>\n<pre>\n\/\/ Fragment 3: Use NULL!\n    \/\/ Create the toolbar.\n    HWND hWndToolbar = CreateWindowEx(\n        0, TOOLBARCLASSNAME, NULL,\n        WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,\n        hWndParent, NULL, NULL, NULL);\n<\/pre>\n<p>\nFurthermore, the customer observed that\n<code>Get&shy;Class&shy;Info(hinst, TOOLBAR&shy;CLASS&shy;NAME, &amp;wc)<\/code>\nworks regardless of whether you pass\nthe process instance or <code>NULL<\/code> for the <code>hinst<\/code> parameter.\n<\/p>\n<p>\nFirst of all, what&#8217;s going on?\nAnd second of all, which of the three methods above is most correct?\n<\/p>\n<p>\nWe can dispatch Fragment&shy;3 easily, because passing\n<code>NULL<\/code> as the instance handle is equivalent\nto passing the process instance handle.\nTherefore, whatever happens in Fragment&shy;3 is explained by whatever\nhappens in Fragment&nbsp;1.\n(Treating a <code>NULL<\/code> instance as a synonym for\nthe process instance is a leftover behavior from 16-bit Windows,\nso I&#8217;m going to declare it a workaround for\nsloppy programming rather than a recommended practice.\nIf you are doing this from the process module itself, then you already\nhave your instance handle, so you should just use it.\nAnd if you are doing this from a DLL, then stop doing it,\nbecause you&#8217;re messing with with somebody else&#8217;s namespace.)\n<\/p>\n<p>\nThe behavior of Fragment&nbsp;2 is easy to explain:\nThe class is registered against the <code>comctl32<\/code>\nlibrary, so naturally, if you create it from that library,\nyou&#8217;ll get the class.\n<\/p>\n<p>\nThe last case is Fragment&nbsp;1:\nEven though we passed the wrong instance handle, we still got\nthe control from <code>comctl32<\/code>.\nWe saw the explanation for this\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/04\/18\/409205.aspx\">\nsome time ago<\/a>:\nIn order to allow the common controls classes to be used\nin dialog templates,\nthey are registered as\n<code>CS_GLOBAL&shy;CLASS<\/code>.\nOne could argue that this is the recommended way of creating the window,\nsince it allows your application to superclass a common control\nby registering a private class with the same name in its own namespace.\nOnly if a custom version is not found in the provided instance\nis the list of global classes consulted.\n(I&#8217;m not saying that <i>I&#8217;m<\/i> arguing that position,\njust that it is a valid position.)\n<\/p>\n<p>\nOkay, so the mystery of the instance handle has been solved.\nBut why does\n<code>Get&shy;Class&shy;Info<\/code> return the class\neven when it&#8217;s registered against some other instance?\n<\/p>\n<p>\nBecause it found the class!\n<code>Get&shy;Class&shy;Info<\/code> uses the same search algorithm\nthat <code>Create&shy;Window<\/code> does,\nand it tells you the class it ultimately found.\nHowever, for compatibility reasons,\nthe <code>WNDCLASS.hInstance<\/code> member is (usually) a copy\nof the <code>HINSTANCE<\/code> you passed to\n<code>Get&shy;Class&shy;Info<\/code>,\nregardless of where the class was ultimately found.\n<\/p>\n<p>\nThe reason for this is that some applications pull tricks like this:\n<\/p>\n<pre>\nWNDCLASS wc;\nGetClassInfo(hinstApp, \"something\", &amp;wc);\n... edit the WNDCLASS structure ...\nUnregisterClass(\"something\", hinstApp);\nRegisterClass(&amp;wc);\n<\/pre>\n<p>\nSuppose that <code>something<\/code> is a global class\nand suppose that the\n<code>WNDCLASS.hInstance<\/code> were set to the instance\nof the module that registered the global class.\nThe application then unregisters its private class\nand registers what it thinks is a replacement private class.\nBut instead, it overwrites the global class.\n<\/p>\n<p>\nOops.\n<\/p>\n<p>\nThe compatibility fix for this is to return\n<code>hinstApp<\/code>\nin the <code>WNDCLASS.hInstance<\/code>\nmember.\nThat way,\nthese programs are tricked into registering a private class\nrather than overwriting a global class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer observed that all of the following code fragments are successful in creating a toolbar common control: \/\/ Fragment 1: Use the process instance \/\/ Create the toolbar. HWND hWndToolbar = CreateWindowEx( 0, TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0, hWndParent, NULL, g_hInst, NULL); \/\/ Fragment 2: Use the comctl32 instance \/\/ [&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],"class_list":["post-5833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer observed that all of the following code fragments are successful in creating a toolbar common control: \/\/ Fragment 1: Use the process instance \/\/ Create the toolbar. HWND hWndToolbar = CreateWindowEx( 0, TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0, hWndParent, NULL, g_hInst, NULL); \/\/ Fragment 2: Use the comctl32 instance \/\/ [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5833","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=5833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5833\/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=5833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}