{"id":94495,"date":"2016-10-12T07:00:00","date_gmt":"2016-10-12T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94495"},"modified":"2019-03-13T10:32:29","modified_gmt":"2019-03-13T17:32:29","slug":"20161012-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161012-00\/?p=94495","title":{"rendered":"Nasty gotcha: The inadvertently named resource"},"content":{"rendered":"<p>In the resources of a Win32 module, <a HREF=\"https:\/\/msdn.microsoft.com\/library\/windows\/desktop\/aa380599(v=vs.85).aspx\">resource files<\/a>, you can identify a resource by number (ordinal) or by name (string). One horrific gotcha of the resource file format is that it doesn&#8217;t require you to quote strings that are used to name resources. <\/p>\n<p>Suppose you have this resource header file. <\/p>\n<pre>\n\/\/ resource.h\n\n#define IDD_ENTERPASSWORD 100\n<\/pre>\n<p>and you use it in your resource file like this: <\/p>\n<pre>\n#include &lt;resource.h&gt;\n\nIDD_ENTERPASWORD DIALOG ...\nBEGIN\n    ...\nEND\n<\/pre>\n<p>And your code tries to use the dialog like this: <\/p>\n<pre>\nDialogBox(g_hinst, MAKEINTRESOURCE(IDD_ENTERPASSWORD),\n          hwndParent, DialogProc);\n<\/pre>\n<p>Everything compiles, but the <code>Dialog&shy;Box<\/code> function returns <code>-1<\/code> without displaying any dialog box. Your breakpoint on the <code>Dialog&shy;Proc<\/code> is never hit. What&#8217;s going on? <\/p>\n<p>What&#8217;s going on is that in your resource file, you misspelled &#8220;password&#8221;. <\/p>\n<pre>\nIDD_ENTER<u>PASWORD<\/u> DIALOG ...\n<\/pre>\n<p>This typo was not reported by the resource compile because of the strange rule that named resources permit but do not require quotation marks around the name. If you omit the quotation marks, the resource compile will &#8220;helpfully&#8221; insert them for you. Since <code>IDD_ENTER&shy;PASWORD<\/code> is not defined anywhere, the resource compile assumes you meant <\/p>\n<pre>\n\"IDD_ENTERPASWORD\" DIALOG ...\n<\/pre>\n<p>and generates a <i>named<\/i> dialog resource called <code>\"IDD_ENTERPASWORD\"<\/code>. If you did this on purpose, then the way you would access the dialog box would be <\/p>\n<pre>\nDialogBox(g_hinst, <font COLOR=\"blue\">TEXT(\"IDD_ENTERPASWORD\")<\/font>,\n          hwndParent, DialogProc);\n<\/pre>\n<p>But you didn&#8217;t misspell the symbol on purpose. It was a mistake. You meant <code>IDD_ENTER&shy;PASSWORD<\/code>. And your misspelling cost you dearly: The resource was given the wrong identifier. <\/p>\n<p>Sucks to be you. <\/p>\n<p>The diagnosis for this class of problems is to verify that the dialog box you describe does indeed exist. <\/p>\n<pre>\n\/\/ Diagnosing the problem: Let's see if the resource exists.\n\/\/ DialogBox(<u>g_hinst<\/u>, <u>MAKEINTRESOURCE(IDD_ENTERPASSWORD)<\/u>, ...)\n\nauto res = <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">FindResource<\/a>(g_hinst,\n                        MAKEINTRESOURCE(IDD_ENTERPASSWORD),\n                        RT_DIALOG);\n<\/pre>\n<p>In the debugger, check the return value of <code>Find&shy;Resource<\/code>. If it&#8217;s <code>nullptr<\/code>, then the reason the <code>Dialog&shy;Box<\/code> function failed is that the resource didn&#8217;t exist. The next step of the investigation would be to find out why the resource isn&#8217;t there. <\/p>\n<p>Maybe you misspelled it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>No such thing as undefined.<\/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-94495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>No such thing as undefined.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94495","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=94495"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94495\/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=94495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}