{"id":2523,"date":"2013-11-29T07:00:00","date_gmt":"2013-11-29T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/11\/29\/why-cant-i-create-my-dialog-with-dialogbox-dialogboxparam-createdialog-createdialogparam-or-the-indirect-versions-of-same\/"},"modified":"2013-11-29T07:00:00","modified_gmt":"2013-11-29T07:00:00","slug":"why-cant-i-create-my-dialog-with-dialogbox-dialogboxparam-createdialog-createdialogparam-or-the-indirect-versions-of-same","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131129-00\/?p=2523","title":{"rendered":"Why can&#039;t I create my dialog with DialogBox, DialogBoxParam, CreateDialog, CreateDialogParam, or the indirect versions of same?"},"content":{"rendered":"<p>\nOne of the purposes of my\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/03\/29\/403298.aspx\">\ndialog manager series<\/a>\nwas to help people diagnose problems with their dialog boxes.\nBut since I embedded the tips inside the series body,\nit&#8217;s hard for people to find them,\nand I still end up answering the same questions over and over.\n<\/p>\n<p>\nSo here it is in a separate article that hopefully people can find.\n<\/p>\n<p>\nWhy your call to <code>Dialog&shy;Box<\/code> or\n<code>Create&shy;Dialog<\/code> is failing.\nThis also goes for <code>CDialog::Do&shy;Modal<\/code>,\nbut perhaps extra so because the MFC source code says\n<\/p>\n<pre>\n* 3) CreateDialogIndirect() did NOT create the window (ie. due to error in template)\n*    and returns NULL.\n<\/pre>\n<p>\neven though the problem is rarely due to an error in the template.\nA wrong comment is worse than no comment at all.&sup1;\n<\/p>\n<p>\nI&#8217;ve decided to put the reasons in most-likely-error-first order\nrather than chronological.\n<\/p>\n<ul>\n<li>Your dialog template uses a custom control which has not\nbeen registered.\n<\/ul>\n<p>\nThis is by far the number one reason why dialog boxes fail\nto be created.\nAs we saw in\n<a HREF=\"blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/03\/31\/404108.aspx\">\nPart&nbsp;3<\/a>,\nif a child control cannot be created from the template,\nthen the dialog creation fails.\n(There is a special dialog box style\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ff729172%28v=vs.85%29.aspx\">\n<code>DS_NO&shy;FAIL&shy;CREATE<\/code><\/a> that tells the dialog manager\nto ignore the error and just continue without the child control.)\n<\/p>\n<p>\nFor example, maybe your dialog box template uses a List View\ncontrol, but you forgot to call\n<code>Init&shy;Common&shy;Controls<\/code> or\n<code>Init&shy;Common&shy;Controls&shy;Ex<\/code>.\nOr it uses a Rich Edit control and you forgot to\nload\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/bb787873%28v=vs.85%29.aspx\">\nthe appropriate library<\/a>.\nOr it contains an ActiveX control but you forgot to\ncall\n<code>Afx&shy;Enable&shy;Control&shy;Container<\/code>\nto enable ActiveX control hosting.\n<\/p>\n<p>\nA special case of this error is where your\ndialog template uses a custom control which was\nregistered in a different activation context.\nIf you are trying to use a control from version&nbsp;6\nof the common controls, then you must\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/bb773175%28v=vs.85%29.aspx\">\nuse the appropriate manifest<\/a>\nand make sure that the manifest&#8217;s activation context is active at the time\nyou call <code>Dialog&shy;Box<\/code> or whatever.\nIf you forget to do this, then you will be using whatever activation\ncontext happens to be lying around at the time you call\n<code>Dialog&shy;Box<\/code>,\nand it may not be the one you want.\n(It&#8217;s like walking up to a computer and looking on the Desktop\nand not seeing the file you created yesterday.\n&#8220;Hey, who deleted my file!&#8221;\nBut you forgot to do a Switch User to your own account.\nYou&#8217;re looking at the Desktop of whoever used the computer last.\nYou&#8217;re in the wrong context.)\n<\/p>\n<ul>\n<li>Your resource name is incorrectly declared in the resource template.\n<\/ul>\n<p>\nOne of the annoying features of the Resource Compiler is that it\nhappily accepts typos!\nFor example, suppose you have the following resource header file:\n<\/p>\n<pre>\n\/\/ ids.h\n#define IDD_BRILLIANT 100\n<\/pre>\n<p>\nand the following resource file:\n<\/p>\n<pre>\n<a HREF=\"http:\/\/thedailywtf.com\/Articles\/The_Brillant_Paula_Bean.aspx\">IDD_BRILLANT<\/a> DIALOG ...\n<\/pre>\n<p>\nSince you misspelled <code>IDD_BRILLIANT<\/code>,\nyou are not in fact creating a dialog box whose ID is 100\n(which is what would have happened if you had spelled it correctly).\nInstead, you&#8217;re creating a dialog whose name is the string\n<code>\"IDD_BRILLANT\"<\/code>.\n<\/p>\n<p>\nLater, your code passes\n<code>MAKE&shy;INT&shy;RESOURCE(IDD_BRILLIANT)<\/code> to ask for dialog 100,\nand the dialog manager says, &#8220;Sorry, I don&#8217;t see one of those.&#8221;\n(To get the typo&#8217;d dialog template, you would have to pass\n<code>TEXT(\"IDD_BRILLANT\")<\/code> as the template name.)\n<\/p>\n<p>\nEven if you spell everything correctly, you will also have this problem\nif you forget to\n<code>#include \"ids.h\"<\/code> in your resource template file\nin the first place!\n<\/p>\n<p>\nTo diagnose this error,\nyou can\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/03\/29\/403298.aspx\">\nadd a diagnostic call to\n<code>Find&shy;Resource<\/code><\/a>\n(or simply trace through the dialog manager&#8217;s call to the same function)\nto see if it can find the resource.\nMany IDEs will let you load a DLL and inspect its resources interactively.\nYou can check the dialog template to see whether it is listed as resource 100\nor as resource <code>\"IDD_BRILLANT\"<\/code>.\n<\/p>\n<ul>\n<li>You passed the wrong instance handle or dialog ID.\n<\/ul>\n<p>\nThis is a generalization of the previous error.\nThe dialog template needs to exist in the module you passed,\nwith the ID or name you passed.\nIf you pass the wrong module or the wrong name,\nthen you&#8217;re not going to find it.\n<\/p>\n<p>\nAn even more generalized version of this error is\nthe case where you forgot to add the dialog to the\nmodule&#8217;s resources in the first place.\n(Maybe you forgot to add the resource file to your project.)\n<\/p>\n<p>\nThe diagnosis for this case is the same as the\ncase of the misspelled dialog identifier.\n<\/p>\n<ul>\n<li>\nThe control refused its creation.\n<\/ul>\n<p>\nIn rare cases, a control may fail its creation\nby returning <code>FALSE<\/code> in response to\nthe <code>WM_NC&shy;CREATE<\/code> message\nor <code>-1<\/code> in response to the\n<code>WM_CREATE<\/code> message.\n<\/p>\n<ul>\n<li>You passed a bad window handle as the <code>hwnd&shy;Parent<\/code>.\n<\/ul>\n<p>\nAn invalid parameter will naturally result in the function failing.\nThis rarely occurs in practice because you should be using the handle\nof a window under your control, so it shouldn&#8217;t be destroyed out\nfrom under you.\n<\/p>\n<ul>\n<li>There is an error in the dialog template.\n<\/ul>\n<p>\nI have never seen this occur.\nThe Resource Compiler is pretty good about\nnot generating erroneous templates.\n<\/p>\n<p>\n&sup1; Perhaps the author meant to use\n<i>e.g.<\/i> (<i>exempli gratia<\/i>,\nwhich means &#8220;for example&#8221;) instead of\n<i>i.e.<\/i> (<i>id est<\/i>, which means\n&#8220;that is&#8221;). As written, the comment is saying\nthat an error in the template is the only reason\nthat <code>Create&shy;Dialog&shy;Indirect<\/code>\ncould have failed, when in fact it is only one\nexample of a failure.\nGetting the two Latin abbreviations confused is not\njust a pedantic error;\nhere, it created genuine confusion and probably\nwasted a lot of developers&#8217; time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the purposes of my dialog manager series was to help people diagnose problems with their dialog boxes. But since I embedded the tips inside the series body, it&#8217;s hard for people to find them, and I still end up answering the same questions over and over. So here it is in a separate [&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-2523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One of the purposes of my dialog manager series was to help people diagnose problems with their dialog boxes. But since I embedded the tips inside the series body, it&#8217;s hard for people to find them, and I still end up answering the same questions over and over. So here it is in a separate [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2523","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=2523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2523\/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=2523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}