{"id":36493,"date":"2005-02-09T07:00:00","date_gmt":"2005-02-09T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/02\/09\/the-dangers-of-filtering-window-messages\/"},"modified":"2005-02-09T07:00:00","modified_gmt":"2005-02-09T07:00:00","slug":"the-dangers-of-filtering-window-messages","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050209-00\/?p=36493","title":{"rendered":"The dangers of filtering window messages"},"content":{"rendered":"<p>\nThe\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/MessagesandMessageQueues\/MessagesandMessageQueuesReference\/MessagesandMessageQueuesFunctions\/SendMessage.asp\"><code>GetMessage<\/code><\/a>\nand\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/MessagesandMessageQueues\/MessagesandMessageQueuesReference\/MessagesandMessageQueuesFunctions\/PeekMessage.asp\"><code>PeekMessage<\/code><\/a> functions\nallow you to pass a filter,\nrestricting the window handle or range of messages\nthat the function will retrieve from the message queue.\nWhile it&#8217;s okay to use these filters,\nmake sure you eventually get around to making an unfiltered call\nso that any straggling messages can come through.\n<\/p>\n<p>\nA common mistake is to use a window-filtered GetMessage in your message loop.\nIn\n<a HREF=\"\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nour scratch program<\/a>,\na window-filtered <code>GetMessage<\/code> would look like this:\n<\/p>\n<pre>\n        while (<i>GetMessage(&amp;msg, <font COLOR=\"blue\">hwnd<\/font>, 0, 0)<\/i>) { \/\/ Wrong!\n            TranslateMessage(&amp;msg);\n            DispatchMessage(&amp;msg);\n        }\n<\/pre>\n<p>\nEven though the program creates but one window,\nthis program is nevertheless incorrect.\n<\/p>\n<p>\n&#8220;How can this be?&#8221; you ask.  &#8220;My program has only one window.\nWhy would there possibly be any messages for any other windows?\nThe filter, while redundant, isn&#8217;t harmful, is it?&#8221;\n<\/p>\n<p>\nMany system services create windows on your behalf.\nFor example, if input method editing is enabled,\nthe method editor may create helper windows to assist in character input.\nIf you initialize COM,\nthen COM may decide to create a helper window\nto assist in inter-thread marshalling.\nIf you use only a filtered GetMessage,\nthen messages destined for these helper windows will never be retrieved,\nand you will be left scratching your head wondering why your program\noccasionally hangs when it tries to perform a drag\/drop operation,\nfor example.\n<\/p>\n<p>\nMoral of the story:\nMake sure your message loop eventually performs an unfiltered\nmessage retrieval so that these services can operate properly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The GetMessage and PeekMessage functions allow you to pass a filter, restricting the window handle or range of messages that the function will retrieve from the message queue. While it&#8217;s okay to use these filters, make sure you eventually get around to making an unfiltered call so that any straggling messages can come through. A [&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-36493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The GetMessage and PeekMessage functions allow you to pass a filter, restricting the window handle or range of messages that the function will retrieve from the message queue. While it&#8217;s okay to use these filters, make sure you eventually get around to making an unfiltered call so that any straggling messages can come through. A [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36493","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=36493"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36493\/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=36493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}