title: Exception thrown when WndProc is overloaded
body: <p>I'm trying to get at the Close event of the .NET WebBrowser type, which doesn't seem to work out of the box. (<strong>EDIT:</strong> This event is emitted when the <code>window.close()</code> call is issued in a script running in the browser.)</p>
  
  <p>One solution I've seen is to <a href="http://blogs.artinsoft.net/Mrojas/archive/2009/05/21/Extended-WebBrowser-Control-Series-WebBrowser-Control-and-windowClose%28%29.aspx" rel="nofollow noreferrer">extend the <code>WebBrowser</code> class and <code>override</code> the <code>WndProc</code> method</a>.</p>
  
  <p>My extension code is as follows:</p>
  
  <pre><code>type internal ExtendedBrowser () = class
      inherit System.Windows.Forms.WebBrowser()
  
      let WM_PARENTNOTIFY : int = 0x0210
      let WM_DESTROY      : int = 0x0002
  
      let closed : Event&lt;unit&gt; = new Event&lt;unit&gt;()
  
      do ()
  
      [&lt;System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")&gt;]
      override this.WndProc (msg : Message byref) =
          match msg.Msg with
          | wm when wm = WM_PARENTNOTIFY -&gt;
              if (not base.DesignMode) &amp;&amp; (msg.WParam.ToInt32() = WM_DESTROY)
              then closed.Trigger()
              base.DefWndProc(ref msg)
          | _ -&gt;
              base.WndProc(ref msg)
  
      member this.Closed = closed.Publish
  end
  </code></pre>
  
  <p>This ends up causing an exception to be thrown when an instance of the type is accessed:</p>
  
  <pre><code>Unhandled Exception: System.Reflection.TargetInvocationException: Unable to get the window handle for the 'ExtendedBrowser' control. Windowless ActiveX controls are not supported. ---&gt; System.ComponentModel.Win32Exception: Error creating window handle.
     at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
     at System.Windows.Forms.Control.CreateHandle()
     at System.Windows.Forms.Control.get_Handle()
     at System.Windows.Forms.WebBrowserBase.DoVerb(Int32 verb)
     at System.Windows.Forms.WebBrowserBase.TransitionFromRunningToInPlaceActive()
  
     --- End of inner exception stack trace ---
     at System.Windows.Forms.WebBrowserBase.TransitionFromRunningToInPlaceActive()
  
     at System.Windows.Forms.WebBrowserBase.TransitionUpTo(AXState state)
     at System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
     at System.Windows.Forms.WebBrowser.PerformNavigate2(Object&amp; URL, Object&amp; flag
  s, Object&amp; targetFrameName, Object&amp; postData, Object&amp; headers)
     at System.Windows.Forms.WebBrowser.Navigate(String urlString)
     at [PRODUCT].Application.WebBrowser..ctor() in C:\[PATH]\WebBrowser.fs:line 107
     at Program.Main.main(String[] args) in C:\[PATH]\Program.fs:line 79
  Press any key to continue . . .
  </code></pre>
  
  <p>Currently it's erroring on a call to <code>Navigate("about:blank")</code> (the first access of the instance after its construction). I can comment out the <code>WndProc</code> override and things work fine (besides missing the close event).</p>
  
  <p>Someone said you had to <a href="https://stackoverflow.com/questions/12010296/net-4-0-accessviolationexception-and-wndproc">put the security attribute on the <code>WndProc</code> override</a> so I did that and it didn't fix things.</p>
  
  <p>Someone else said you can <a href="https://stackoverflow.com/a/12000538/225292">disable the DEP</a>, but I tried and it didn't let me exempt the EXE.</p>
  
  <p>An instance of the extension is being created in a wrapper for the browser (also called WebBrowser) and an instance of this is being created in my <code>main</code>, which is runing in an [STAThread] (which also seems to be required).</p>
  
  <p>Does anyone know what could be going wrong?</p>
  
  <p>(What I'm really after is a way to get notification of the close event, so if someone knows an alternate route to that I'd be happy to hear it.)</p>