title: IShellWindows::FindWindowSW returns S_FALSE for desktop if explorer restarted
body: <p>I have a program that passes a command line to the explorer process to execute in the user's non-elevated desktop context. Part of doing that requires using IShellWindows::FindWindowSW to search for the desktop window, as can be seen in <a href="https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/Win7Samples/winui/shell/appplatform/ExecInExplorer/ExecInExplorer.cpp#L31" rel="nofollow noreferrer">this ExecInExplorer example</a> from the Windows 7 SDK.</p>
  <p>If for some reason explorer is restarted (such as 'Exit Explorer' and restart [2]) and the old explorer doesn't actually exit then FindWindowSW returns S_FALSE for the desktop window. In this case the new explorer process owns the desktop and the old process (which should have terminated) does not. I suspect FindWindowSW is searching only the old explorer process.</p>
  <p>If there are multiple explorers running is there any way to search all of them for IDispatch to the desktop?</p>
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;shlwapi.h&gt;
  #include &lt;shlobj.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  
  #pragma comment(lib, &quot;shlwapi.lib&quot;)
  
  
  int main(int argc, char *argv[]) {
    (argv); (argc);
    init();
    
    HRESULT hr;
    IShellWindows *psw;
    
    hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr))
    {
      hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&amp;psw));
      if (SUCCEEDED(hr))
      {
        HWND hwnd;
        IDispatch* pdisp;
        VARIANT vEmpty = {}; // VT_EMPTY
        HRESULT f = psw-&gt;FindWindowSW(&amp;vEmpty, &amp;vEmpty, SWC_DESKTOP, (long*)&amp;hwnd, SWFO_NEEDDISPATCH, &amp;pdisp);
        if(f == S_OK)
          printf(&quot;S_OK, hwnd: 0x%lx\n&quot;, *(long*)&amp;hwnd);
        else if(f == S_FALSE)
          printf(&quot;S_FALSE\n&quot;);
        else
          printf(&quot;0x%lx\n&quot;, f);
      }
    }
    return 0;
  }
  </code></pre>
  <p>[2]: (Sometimes when I am developing I will 'Exit Explorer'  (Hold down CTRL+SHIFT and right-click on an empty part of the start menu) and then restart it from a command prompt. Occasionally explorer despite exiting (ie the taskbar disappears) doesn't actually terminate which means when I start explorer again then more than one explorer is running. The old explorer still running is not the separate process that can be used for displaying folders. I don't have a way to reproduce this, it seems arbitrary.)</p>