title: Why is EnumChildWindows skipping children?
body: <p>I'm getting strange behavior when it comes to using the Windows API method EnumChildWindows. It seems to not be picking up a section of children windows. When I drill down using Spy++ I can see the children, but when I execute my code, it doesn't return the ones I see in Spy++.</p>
  
  <p>What I see in Spy++
  <a href="http://img24.imageshack.us/img24/9264/spyhandles.png">What I see in Spy++ http://img24.imageshack.us/img24/9264/spyhandles.png</a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
  
      [DllImport("user32")]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
  
      public static List&lt;IntPtr&gt; GetChildWindows(IntPtr parent)
      {
          List&lt;IntPtr&gt; result = new List&lt;IntPtr&gt;();
          GCHandle listHandle = GCHandle.Alloc(result);
          try
          {
              EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
              EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
          }
          finally
          {
              if (listHandle.IsAllocated)
                  listHandle.Free();
          }
          return result;
      }
  
      private static bool EnumWindow(IntPtr handle, IntPtr pointer)
      {
          GCHandle gch = GCHandle.FromIntPtr(pointer);
          List&lt;IntPtr&gt; list = gch.Target as List&lt;IntPtr&gt;;
          if (list == null)
              throw new InvalidCastException("GCHandle Target could not be cast as List&lt;IntPtr&gt;");
  
          list.Add(handle);            
          return true;
      }
  </code></pre>
  
  <p>Is there a reason as to why the highlighted red section in the screenshot above would not get populated into my collection (<code>List&lt;IntPtr&gt;</code>) when calling EnumChildWindows?</p>