{"id":35873,"date":"2005-04-18T09:03:59","date_gmt":"2005-04-18T09:03:59","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/04\/18\/what-is-the-hinstance-passed-to-createwindow-and-registerclass-used-for\/"},"modified":"2005-04-18T09:03:59","modified_gmt":"2005-04-18T09:03:59","slug":"what-is-the-hinstance-passed-to-createwindow-and-registerclass-used-for","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050418-59\/?p=35873","title":{"rendered":"What is the HINSTANCE passed to CreateWindow and RegisterClass used for?"},"content":{"rendered":"<p>\nOne of the less-understood parameters to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windows\/windowreference\/windowfunctions\/createwindow.asp\">\nthe <code>CreateWindow<\/code> function<\/a>\nand\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windowclasses\/windowclassreference\/windowclassfunctions\/registerclass.asp\">\nthe <code>RegisterClass<\/code> function<\/a>\nis the <code>HINSTANCE<\/code> (either passed as a parameter\nor as part of\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windowclasses\/windowclassreference\/windowclassstructures\/wndclass.asp\">\nthe <code>WNDCLASS<\/code> structure<\/a>).\n<\/p>\n<p>\nThe window class name is not sufficient to identify the class uniquely.\nEach process has its own window class list,\nand each entry in the window class list consists of an instance handle\nand a class name.\nFor example, here&#8217;s what the window class list might look like if a\nprogram has two DLLs, both of which register a class name &#8220;MyClass&#8221;,\npassing the DLL&#8217;s handle as the <code>HINSTANCE<\/code>.\n<\/p>\n<table>\n<tr>\n<th><\/th>\n<th>HINSTANCE<\/th>\n<th>Class name<\/th>\n<\/tr>\n<tr>\n<td>1.<\/td>\n<td>USER32.DLL<\/td>\n<td>Static<\/td>\n<\/tr>\n<tr>\n<td>2.<\/td>\n<td>USER32.DLL<\/td>\n<td>Button<\/td>\n<\/tr>\n<tr>\n<td>3.<\/td>\n<td>USER32.DLL<\/td>\n<td>Listbox<\/td>\n<\/tr>\n<tr>\n<td>4.<\/td>\n<td>USER32.DLL<\/td>\n<td>Combobox<\/td>\n<\/tr>\n<tr>\n<td>5.<\/td>\n<td>USER32.DLL<\/td>\n<td>Edit<\/td>\n<\/tr>\n<tr>\n<td>6.<\/td>\n<td>A.DLL<\/td>\n<td>MyClass<\/td>\n<\/tr>\n<tr>\n<td>7.<\/td>\n<td>B.DLL<\/td>\n<td>MyClass<\/td>\n<\/tr>\n<\/table>\n<p>\nWhen it comes time to create a window, each module then passes\nits own <code>HINSTANCE<\/code> when creating the window,\nand the window manager uses the combination of the instance handle\nand the class name to look up the class.\n<\/p>\n<pre>\nCreateWindow(\"MyClass\", ..., hinstA, ...); \/\/ creates class 6\nCreateWindow(\"MyClass\", ..., hinstB, ...); \/\/ creates class 7\nCreateWindow(\"MyClass\", ..., hinstC, ...); \/\/ fails\n<\/pre>\n<p>\nThis is why it is okay if multiple DLLs all register a class\ncalled &#8220;MyClass&#8221;; the instance handle is used to tell them apart.\n<\/p>\n<p>\nThere is an exception to the above rule, however.\nIf you pass the <code>CS_GLOBALCLASS<\/code> flag when registering\nthe class, then the window manager will ignore the instance handle\nwhen looking for your class.  All of the USER32 classes are registered\nas global.  Consequently, all of the following calls create the\nUSER32 edit control:\n<\/p>\n<pre>\nCreateWindow(\"edit\", ..., hinstA, ...);\nCreateWindow(\"edit\", ..., hinstB, ...);\nCreateWindow(\"edit\", ..., hinstC, ...);\n<\/pre>\n<p>\nIf you are registering a class for other modules to use in\ndialog boxes, you need to register as <code>CS_GLOBALCLASS<\/code>,\nbecause as we saw earlier\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/03\/30\/403711.aspx\">\nthe internal <code>CreateWindow<\/code> call performed during dialog\nbox creation to create the controls<\/a>\npasses the dialog&#8217;s <code>HINSTANCE<\/code> as the\n<code>HINSTANCE<\/code> parameter.\nSince the dialog instance handle is typically the DLL that is\ncreating the dialog\n(since\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/03\/29\/403298.aspx\">\nthat same <code>HINSTANCE<\/code> is used to look up the template<\/a>),\nfailing to register with the <code>CS_GLOBALCLASS<\/code> flag\nmeans that the window class lookup will not find the class\nsince it&#8217;s registered under the instance handle of the DLL that\nprovided the class, not the one that is using it.\n<\/p>\n<p>\nIn 16-bit Windows, the instance handle did other things, too,\nbut they are no longer relevant to Win32.\n<\/p>\n<p>\n<a HREF=\"http:\/\/www.experts-exchange.com\/Programming\/Programming_Platforms\/Win_Prog\/Q_20331499.html\">\nA common mistake<\/a> is to pass the\n<code>HINSTANCE<\/code> of some other module\n(typically, the primary executable)\nwhen registering a window class.\nNow that you understand what the\n<code>HINSTANCE<\/code> is used for,\nyou should be able to\nexplain the consequences of registering a class with the wrong\n<code>HINSTANCE<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the less-understood parameters to the CreateWindow function and the RegisterClass function is the HINSTANCE (either passed as a parameter or as part of the WNDCLASS structure). The window class name is not sufficient to identify the class uniquely. Each process has its own window class list, and each entry in the window class [&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-35873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One of the less-understood parameters to the CreateWindow function and the RegisterClass function is the HINSTANCE (either passed as a parameter or as part of the WNDCLASS structure). The window class name is not sufficient to identify the class uniquely. Each process has its own window class list, and each entry in the window class [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35873","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=35873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35873\/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=35873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}