{"id":30893,"date":"2006-06-13T10:00:05","date_gmt":"2006-06-13T10:00:05","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/06\/13\/fumbling-around-in-the-dark-and-stumbling-across-the-wrong-solution\/"},"modified":"2006-06-13T10:00:05","modified_gmt":"2006-06-13T10:00:05","slug":"fumbling-around-in-the-dark-and-stumbling-across-the-wrong-solution","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060613-05\/?p=30893","title":{"rendered":"Fumbling around in the dark and stumbling across the wrong solution"},"content":{"rendered":"<p>\nI don&#8217;t mean to pick on this series of entries,\nbut it illustrates an interesting pattern of\nstumbling across the wrong &#8220;solution&#8221;.\n<\/p>\n<p>\n<a HREF=\"http:\/\/dotnetjunkies.com\/WebLog\/victorv\/archive\/2004\/09\/13\/25380.aspx\">\nThe series begins<\/a>\nby attempting to trigger the system&#8217;s monitor blank timeout\nby posting a message to the desktop window.\nAs we saw earlier,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/24\/79212.aspx\">\nthe desktop window is a very special window<\/a>\nand as a rule\nshould be avoided, since it won&#8217;t behave like windows created\nby applications.\nIn particular, the author tried to post a message to the desktop\nwindow.\nThis used to work in the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/10\/21\/483438.aspx\">\nhistorically open world of the window manager<\/a>,\nbut security and robustness concerns have come to take\npriority over compatibility.\nIn Windows&nbsp;XP&nbsp;SP2, the desktop window resists being disabled\nbecause programs were doing it inadvertently,\nand\nit appears that the desktop also resists having messages posted to it.\nMy guess is that this was done as a way to strengthen protection against\nshatter attacks.\nThis did improve robustness and stability,\nbut\n<a HREF=\"http:\/\/dotnetjunkies.com\/WebLog\/victorv\/archive\/2004\/09\/13\/25386.aspx\">\nit also broke the article&#8217;s dubious <code>PostMessage<\/code> hack<\/a>.\n<\/p>\n<p>\n<a HREF=\"http:\/\/dotnetjunkies.com\/WebLog\/victorv\/archive\/2004\/09\/13\/25394.aspx\">\nEnter round three<\/a>,\nwherein the author fumbled around for other windows the\nmonitor blank timeout message could be posted to,\nand eventually\nthe author found that\nposting the message to the mysterious window\n<code>HWND_TOPMOST = -1<\/code>\nseemed to do the trick.\n<\/p>\n<p>\nI knew in the back of my mind that people developed software\nthis way, but the hopeful part of my brain continued to wish\nthat it was merely taking place in a fantasy world.\nMaking up intentionally invalid parameters\nand seeing what happens falls into the\ncategory of malicious goofing around,\nnot in the realm of software engineering and design.\nEven if you find something that seems to work,\nyou certainly wouldn&#8217;t design a product around it!\n<\/p>\n<p>\n(Similarly, I&#8217;ve seen people ask questions like\n&#8220;What does message 49251 mean?&#8221;\nThis is the reverse case: Seeing a made-up number and\nattempting to assign meaning to it.\nMessage numbers starting at <code>0xC000<\/code>\n(decimal 49152) are messages registered via\n<code>RegisterWindowMessage<\/code>.\nThe numerical value of the message associated with a\nregistered window message is unpredictable and varies\nfrom desktop to desktop.\nThe only guarantee is that it will remain consistent\nwithin a single desktop.)\n<\/p>\n<p>\nIf you look more carefully at what the author stumbled\nacross, you&#8217;ll see that the &#8220;solution&#8221; is actually another bug.\nIt so happens that the numerical value\n<code>-1<\/code>\nfor\na window handle is suspiciously close to the value of\n<code>HWND_BROADCAST<\/code>:\n<\/p>\n<pre>\n#define HWND_BROADCAST  ((HWND)0xffff)\n<\/pre>\n<p>\nIt so happens that internally, the window manager\nsupports\n<code>(HWND)-1<\/code>\nas an alternative value\nfor <code>HWND_BROADCAST<\/code>.\n(I leave you to speculate why.)\nAs a result, what the author actually is doing is broadcasting\nthe monitor power-off message to all top-level windows!\nAs we saw before, broadcasting messages is a very dangerous\nbusiness, and in this case,\nthe author is just lucky that all the windows on the desktop\ninterpret the message the same way, that it is safe to\nprocess the message multiple times,\nand none of the windows\nperform any special filtering for that message.\n(Another author\n<a HREF=\"http:\/\/www.codeproject.com\/system\/display_states.asp\">\nstumbled across the same incorrect &#8220;solution&#8221;<\/a>\nbut didn&#8217;t provide any insight into the process by which the\nresult was arrived at.\n<a HREF=\"http:\/\/www.codeproject.com\/useritems\/Monitor_management_guide.asp\">\nYet another author sort of found some issues but didn&#8217;t quite put them\nall together<\/a>.)\n<\/p>\n<p>\nFor example, a presentation program might want to suppress\nmonitor power-off when it is the foreground window by\ntrapping the message and turning the monitor back on.\nIf such a program happens to be running,\nbroadcasting the power-off message to all top-level windows\nwould turn off the monitor for all the windows that\ndeferred to system default behavior, but when that presentation\nprogram received the message, it would turn the monitor back on.\nNow you&#8217;re at the mercy of the order in which the windows process\nthat broadcast message.\nWhen the presentation program processes the message,\nthe monitor will turn back on,\nand if that program happens to be the last one to process\nthe message (say, it got paged out and was slow to page back in),\nthen the monitor will merely blink off and back on.\n<\/p>\n<p>\nThe correct solution is not to post messages to random windows.\nIf you want the message to go through window message\ndefault processing, create a window and process it yourself.\nDon&#8217;t try to trick some other window (or in this case,\nhundreds of other windows simultaneously) into doing it for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t mean to pick on this series of entries, but it illustrates an interesting pattern of stumbling across the wrong &#8220;solution&#8221;. The series begins by attempting to trigger the system&#8217;s monitor blank timeout by posting a message to the desktop window. As we saw earlier, the desktop window is a very special window and [&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":[26],"class_list":["post-30893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>I don&#8217;t mean to pick on this series of entries, but it illustrates an interesting pattern of stumbling across the wrong &#8220;solution&#8221;. The series begins by attempting to trigger the system&#8217;s monitor blank timeout by posting a message to the desktop window. As we saw earlier, the desktop window is a very special window and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/30893","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=30893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/30893\/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=30893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=30893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=30893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}