{"id":28313,"date":"2007-01-23T10:00:04","date_gmt":"2007-01-23T10:00:04","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/01\/23\/non-psychic-debugging-if-you-cant-find-something-make-sure-youre-looking-in-the-right-place\/"},"modified":"2007-01-23T10:00:04","modified_gmt":"2007-01-23T10:00:04","slug":"non-psychic-debugging-if-you-cant-find-something-make-sure-youre-looking-in-the-right-place","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070123-04\/?p=28313","title":{"rendered":"Non-psychic debugging: If you can&#039;t find something, make sure you&#039;re looking in the right place"},"content":{"rendered":"<p>\nThis isn&#8217;t psychic debugging,\nbut it&#8217;s the sort of dumb mistake everybody makes\nand which you can&#8217;t see when re-reading your code\nbecause your brain shows you what you want to see,\nnot what&#8217;s really there.\n<\/p>\n<blockquote CLASS=\"q\"><p>\nI&#8217;m trying to respond to the <code>PSN_QUERYINITIALFOCUS<\/code>\nnotification, but it&#8217;s not working. What am I doing wrong?<\/p>\n<pre>\nINT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg,\n                         WPARAM wParam, LPARAM lParam)\n{\n switch (uMsg) {\n ...\n case WM_NOTIFY:\n  switch (reinterpret_cast&lt;NMHDR*&gt;(lParam)-&gt;code) {\n  ...\n  case PSN_QUERYINITIALFOCUS:\n   {\n    PSHNOTIFY *ppsn = reinterpret_cast&lt;PSHNOTIFY*&gt;(lParam);\n    SetWindowLongPtr(ppsn-&gt;hdr.hwndFrom, DWLP_MSGRESULT,\n                    (LPARAM)GetDlgItem(ppsn-&gt;hdr.hwndFrom, IDC_MYCONTROL));\n   }\n   return TRUE;\n  ...\n  }\n  break;\n }\n return FALSE;\n}\n<\/pre>\n<\/blockquote>\n<p>\nYou can stare at this code for ages and completely overlook\nthat the wrong window handle is being passed to <code>GetDlgItem<\/code>\nand <code>SetWindowLongPtr<\/code>.\nThe <code>hwndFrom<\/code> of a <code>WM_NOTIFY<\/code> message is\nthe window that is generating the notification; in this case,\nthe <code>PSN_QUERYINITIALFOCUS<\/code> is generated from the\nproperty sheet frame window.\nBut the result of a dialog message needs to be stored\nin the extra bytes of the dialog that&#8217;s\n<strong>receiving<\/strong> the message, not the one that&#8217;s\nsending it.\nAnd when you call <code>GetDlgItem<\/code>, the window manager\nsearches for the control among the children of the window you\npass as the first parameter&mdash;but here, <code>IDC_MYCONTROL<\/code>\nis a child of the property sheet page, not the property sheet frame.\n<\/p>\n<p>\nThe correct code should read\n<\/p>\n<pre>\n    SetWindowLongPtr(hdlg, DWLP_MSGRESULT,\n                    (LPARAM)GetDlgItem(hdlg, IDC_MYCONTROL));\n<\/pre>\n<p>\nThis phenomenon of missing the obvious because your brain shows you\nwhat you want to see (rather than what&#8217;s actually there)\nreminds me of a time\none of my colleagues called me into his office to help figure\nout why one of his loops was iterating only once.\nHe called the function up on the screen and talked me through it.\n&#8220;Okay, now the variables are set up for the loop, so\n<code>while<\/code> we haven&#8217;t found the entry, we grab the\nnext item from the list&#8230;&#8221;\n<\/p>\n<p>\nI hesitatantly interrupted.\n&#8220;Um, &#8216;<code>while<\/code>&#8216;?\nThe code says &#8216;<code>if<\/code>&#8216;.&#8221;\n<\/p>\n<p>\n&#8220;Oops. Um, nevermind. Nothing to see here. Move along now.&#8221;\n<\/p>\n<p>\nThis is the same reason you want to have somebody else\nproofread your writing.\nSince you wrote it, your brain will show you what you meant to write,\nnot necessarily what you actually wrote.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This isn&#8217;t psychic debugging, but it&#8217;s the sort of dumb mistake everybody makes and which you can&#8217;t see when re-reading your code because your brain shows you what you want to see, not what&#8217;s really there. I&#8217;m trying to respond to the PSN_QUERYINITIALFOCUS notification, but it&#8217;s not working. What am I doing wrong? INT_PTR CALLBACK [&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-28313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>This isn&#8217;t psychic debugging, but it&#8217;s the sort of dumb mistake everybody makes and which you can&#8217;t see when re-reading your code because your brain shows you what you want to see, not what&#8217;s really there. I&#8217;m trying to respond to the PSN_QUERYINITIALFOCUS notification, but it&#8217;s not working. What am I doing wrong? INT_PTR CALLBACK [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28313","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=28313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28313\/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=28313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=28313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=28313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}