{"id":91951,"date":"2015-11-05T07:00:00","date_gmt":"2015-11-05T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20151105-00\/?p=91951\/"},"modified":"2019-03-13T12:21:23","modified_gmt":"2019-03-13T19:21:23","slug":"20151105-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20151105-00\/?p=91951","title":{"rendered":"Sometimes a function returns NULL because NULL really is the answer"},"content":{"rendered":"<p>A customer reported that sometimes the <code>Get&shy;GUI&shy;Thread&shy;Info<\/code> function returned a valid window handle, but sometimes it returned all <code>NULL<\/code>s. <\/p>\n<pre>\n  DWORD dwThreadId = GetWindowThreadProcessId(hwnd, NULL);\n  GUITHREADINFO guiThreadInfo;\n  guiThreadInfo.cbSize = sizeof(GUITHREADINFO);\n  if (GetGUIThreadInfo(dwThreadId, &amp;guiThreadInfo)) {\n   HWND hwndActive = guiThreadInfo.hwndActive;\n   ...\n  }\n<\/pre>\n<p>Most of the time, the call to <code>Get&shy;GUI&shy;Thread&shy;Info<\/code> succeeds and obtains <code>hwndActive<\/code> successfully. But sometimes, <code>Get&shy;GUI&shy;Thread&shy;Info<\/code> succeeds, but <code>guiThreadInfo.hwndActive<\/code> is <code>NULL<\/code>. In fact, aside from the <code>cbSize<\/code>, all the members of the <code>guiThreadInfo<\/code> are <code>NULL<\/code> or zero. &#8220;Under what circumstances will <code>Get&shy;GUI&shy;Thread&shy;Info<\/code> succeed but return no data?&#8221; <\/p>\n<p>I suggested that they start with the obvious: Is it possible that the function is correct and the thread has no active window? <\/p>\n<p>The customer confessed that they were too quick to assume that there was a problem with the <code>Get&shy;GUI&shy;Thread&shy;Info<\/code> function just because it reported no data. It turns out that, in fact, there was no data to report. <\/p>\n<p>The root cause was that another thread in their program called <code>Set&shy;Window&shy;Pos<\/code> and didn&#8217;t pass the <code>SWP_NO&shy;ACTIVATE<\/code> flag. As a result, that thread stole activation from the first thread, so when they got around to asking the first thread, &#8220;Tell me about your active window, your focus window, your caret window, and your caret location,&#8221; it replied, &#8220;I don&#8217;t have any of those things!&#8221; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is nothing there.<\/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-91951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There is nothing there.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91951","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=91951"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91951\/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=91951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=91951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=91951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}