title: WinForms main window handle
body: <p>In my winforms application I am trying to get a main window handle, so I can set it as parent to my wpf modal window. I am not too experienced with winforms, so after a bit of googling I found two ways to get it. </p>
  
  <ol>
  <li><pre><code>System.Windows.Forms.Application.OpenForms[0].Handle
  </code></pre></li>
  <li><pre><code>System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
  </code></pre></li>
  </ol>
  
  <p>(1) seems to always return the same value which appears to be correct (at least my modal window behaves as expected), while (2) sometimes returns the same value as (1), but sometimes - an entirely different pointer, which does not seem to work (my modal window appears on top of every other window, not just the parent window). </p>
  
  <p>Can someone explain the difference between the two methods? Is it normal that sometimes they return different results?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>In case anyone else is wondering: once you get the handle, you can use it by creating <code>WindowInteropHelper</code> class:</p>
  
  <pre><code>public static void SetInteropParent(this Window wpfDialogWindow, IntPtr winformsParentHandle)
  {
      new WindowInteropHelper(wpdDialogWindow) { Owner = winformsParentHandle }; 
  }  
  </code></pre>