title: Why can't I get the RootVisual Of External App?
body: <p>I am working on an automated testing framework in WPF.  I am finding that while it is possible to automate most things using the basic WPF automation framework, it is very difficult to get down to the fine grained details of what is happening at the UI level. I need to be able to see things like the properties of the DataContext, properties of controls, and so on. I know this is possible because Snoop can do it. Snoop allows you to traverse the entire Visual Tree of any WPF app. I need this functionality. So, I wrote this code:</p>
  
  <pre><code>public async Visual GetAppRootVisual()
  {
      var allProcesses = Process.GetProcesses();
      var filteredProcess = allProcesses.Where(p =&gt; p.ProcessName.Contains(ProcessSearchText)).First();
      var windowHandle = filteredProcess.MainWindowHandle;
      var hwndSource = HwndSource.FromHwnd(windowHandle);
      return hwndSource.RootVisual;
  }
  </code></pre>
  
  <p>The code works up until the second last line. The second last line returns null, but I can't figure out why. The windowHandle is returned, but the HwndSource is not returned. <strong>What is going wrong here?</strong></p>