title: Native window hierarchy and class names in C#
body: <p>I am creating a function to find specific windows given a window hierarchy as follows:</p>
  
  <pre><code>protected bool MapWindowHierarchy (ATS.Library.Window window)
  {
      bool result = false;
      List&lt;Process&gt; processes = null;
  
      processes = GetProcesses().ToList();
  
      processes.ForEach
      (
          process =&gt;
          {
              if (process.MainWindowHandle == window.Handle)
              {
                  // Populate window properties.
                  //  Get child windows with filled properties.
              }
          }
      );
  
      return (result);
  }
  
  protected bool MapWindowHierarchy (List&lt;ATS.Library.Window&gt; windows)
  {
      return (windows.All(window =&gt; this.MapWindowHierarchy(window)));
  }
  
  public sealed class Window
  {
      public IntPtr Handle { get; set; }
      public string Class { get; set; }
      public Rectangle Bounds { get; set; }
      public Win32Api.User32.ShowWindowCommands WindowState { get; set; }
      public string Caption { get; set; }
      public int Style { get; set; }
      public List&lt;ATS.Library.Window&gt; Windows { get; set; }
  
      public void PopulateFromHandle(IntPtr hWnd)
      {
          // Populate above properties using Win32 API.
      }
  }
  </code></pre>
  
  <p>This is just rough code but the objective is to create a heirarchy using the <code>[Window]</code> class and see if the actual hierarchy using <code>[EnumChildWindows]</code> matches. This apparently works fine but I came across <a href="https://stackoverflow.com/questions/10850485/net-class-name-from-a-window-handle">this SO question</a>, Hans Passant's comment <code>"Every unique window in a desktop session must have a unique Windows class name"</code>, I wasn;t sure how to interpret it (see image below for multiple windows with the same class name). Perhaps what he mentions only applies to managed code (WinForms, WPF, etc.).</p>
  
  <p>Furthermore, I am not sure how to retrieve the class names and text of the windows given I have the handle (hWnd) like Spy++ does (see image below). Notice how the text is retrieved in the tree view but not the <code>[Find Window]</code> dialog.</p>
  
  <p><img src="https://i.stack.imgur.com/clvVi.png" alt="Spy++"></p>
  
  <p>Question:</p>
  
  <ul>
  <li>Should I be worried about Han's comment since I am only targeting native apps?</li>
  <li>How to get the class name given a window's handle?</li>
  <li>How to get the window text given a window's handle (like in the image)?</li>
  </ul>