{"id":96266,"date":"2017-06-02T07:00:00","date_gmt":"2017-06-02T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96266"},"modified":"2019-03-13T01:12:25","modified_gmt":"2019-03-13T08:12:25","slug":"20170602-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170602-00\/?p=96266","title":{"rendered":"How likely is it that a window will receive a WM_NULL message out of the blue?"},"content":{"rendered":"<p>A customer discovered a bug in their control that resulted in a crash: <\/p>\n<pre>\nLRESULT CALLBACK MyWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\n{\n  switch (uMsg) {\n  ...\n  default:\n    if (uMsg == g_customRegisteredMessage) {\n      \/\/ For this message, the lParam is a pointer\n      return HandleCustomMessage((SOMETHING*)lParam);\n    }\n    break;\n  }\n  return DefWindowProc(hwnd, uMsg, wParam, lParam);\n}\n<\/pre>\n<p>The problem is that under certain conditions, the control doesn&#8217;t initialize the <code>g_custom&shy;Registered&shy;Message<\/code> variable. If a <code>WM_<\/code><code>NULL<\/code> message arrives, the test <\/p>\n<pre>\n    if (uMsg == g_customRegisteredMessage) {\n<\/pre>\n<p>is <code>true<\/code>, and we take the <code>lParam<\/code> of the <code>WM_<\/code><code>NULL<\/code> message and treat it as a pointer. Since the <code>lParam<\/code> of the <code>WM_<\/code><code>NULL<\/code> message is usually zero, this causes the program to crash with a null pointer. <\/p>\n<p>The customer fully acknowledged the bug. But their question was one of risk management. How likely is a window going to receive the <code>WM_<\/code><code>NULL<\/code> mesasge? Knowing the likelihood of the scenario would help them decide how critical the fix is. (And they weren&#8217;t able to reproduce the problem in-house, so as far as they could determine, the likelihood was effectively zero. And yet it was happening.) <\/p>\n<p>The <code>WM_<\/code><code>NULL<\/code> message is not a common one, but it&#8217;s not uncommon either. Posting a <code>WM_<\/code><code>NULL<\/code> is usually done by a window to itself in order to <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">wake up its message loop<\/a>. This is typically done when the program has a custom message loop, and it needs some of the non-message code to run. We saw an example of this <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20050405-46\/?p=35973\">some time ago<\/a> where we posted a <code>WM_<\/code><code>NULL<\/code> to let our message loop know that the pseudo-dialog has exited. <\/p>\n<p>Posted <code>WM_<\/code><code>NULL<\/code> messages are usually  done from a program to itself, and they are usually posted as thread messages, not window messages, so they don&#8217;t normally come through the window procedure. <\/p>\n<p>Sending a <code>WM_<\/code><code>NULL<\/code> is a different story, though. It is a relative common technique to send a <code>WM_<\/code><code>NULL<\/code> message to a window for the purpose of checking whether the window is responding to messages. We used it to <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20161118-00\/?p=94745\">wait for a window to finish processing a foreground change<\/a>. Some system monitoring tools will periodically call <code>Send&shy;Message&shy;Timeout<\/code> to send a <code>WM_<\/code><code>NULL<\/code> to all windows, just to see if they are responding. Windows UI Automation uses <code>WM_<\/code><code>NULL<\/code> messages help determine the <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/aa359460(v=vs.85).aspx\">window interaction state<\/a>. <\/p>\n<p>The customer could try running system monitoring tools or accessibility tools to increase the likelihood of receiving a <code>WM_<\/code><code>NULL<\/code> message under normal use. (I mean, sure, they could write a program that explicitly sends a <code>WM_<\/code><code>NULL<\/code> message to their window, but that wouldn&#8217;t be anything a normal end-user would have.) <\/p>\n<p>I suspect the customer will bump up the priority of this issue due to the accessibility angle. People who use accessibility tools tend to really need them. It&#8217;s not like you can tell a person with poor visual acuity, &#8220;Oh, just suck it up for a while.&#8221; <\/p>\n<p><b>Bonus chatter<\/b>: The customer wrote back. After further investigation, they found that the problem was traced to a third party tool that their client was using, specifically <a HREF=\"https:\/\/github.com\/Maximus5\/ConEmu\/blob\/1b6bb22e8be3b0d29b10f23abfdd31aed44a756f\/src\/common\/DefTermBase.h#L404\">this line of code<\/a> that sends a <code>WM_<\/code><code>NULL<\/code> message to the foreground window to determine whether it is responding. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not entirely unlikely, let&#8217;s put it that way.<\/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-96266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Not entirely unlikely, let&#8217;s put it that way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96266","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=96266"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96266\/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=96266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}