Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:32:55+00:00 2026-06-17T05:32:55+00:00

I’m trying to get at the Close event of the .NET WebBrowser type, which

  • 0

I’m trying to get at the Close event of the .NET WebBrowser type, which doesn’t seem to work out of the box. (EDIT: This event is emitted when the window.close() call is issued in a script running in the browser.)

One solution I’ve seen is to extend the WebBrowser class and override the WndProc method.

My extension code is as follows:

type internal ExtendedBrowser () = class
    inherit System.Windows.Forms.WebBrowser()

    let WM_PARENTNOTIFY : int = 0x0210
    let WM_DESTROY      : int = 0x0002

    let closed : Event<unit> = new Event<unit>()

    do ()

    [<System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")>]
    override this.WndProc (msg : Message byref) =
        match msg.Msg with
        | wm when wm = WM_PARENTNOTIFY ->
            if (not base.DesignMode) && (msg.WParam.ToInt32() = WM_DESTROY)
            then closed.Trigger()
            base.DefWndProc(ref msg)
        | _ ->
            base.WndProc(ref msg)

    member this.Closed = closed.Publish
end

This ends up causing an exception to be thrown when an instance of the type is accessed:

Unhandled Exception: System.Reflection.TargetInvocationException: Unable to get the window handle for the 'ExtendedBrowser' control. Windowless ActiveX controls are not supported. ---> 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& URL, Object& flag
s, Object& targetFrameName, Object& postData, Object& 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 . . .

Currently it’s erroring on a call to Navigate("about:blank") (the first access of the instance after its construction). I can comment out the WndProc override and things work fine (besides missing the close event).

Someone said you had to put the security attribute on the WndProc override so I did that and it didn’t fix things.

Someone else said you can disable the DEP, but I tried and it didn’t let me exempt the EXE.

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 main, which is runing in an [STAThread] (which also seems to be required).

Does anyone know what could be going wrong?

(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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:32:56+00:00Added an answer on June 17, 2026 at 5:32 am

    I found a post where someone else had had the same problem overriding the WndProc method in F#, and the solution there worked for me; working code is as follows:

    type ExtendedWebBrowser () = class
        inherit System.Windows.Forms.WebBrowser()
    
        let WM_PARENTNOTIFY : int = 0x0210
        let WM_DESTROY      : int = 0x0002
    
        let closed : Event<unit> = new Event<unit>()
    
        do ()
    
        override this.WndProc (msg : Message byref) =
            match msg.Msg with
            | wm when wm = WM_PARENTNOTIFY ->
                if (not base.DesignMode) && (msg.WParam.ToInt32() = WM_DESTROY)
                then closed.Trigger()
                base.DefWndProc(&msg)
            | _ ->
                base.WndProc(&msg)
    
        member this.Closed = closed.Publish
    end
    

    It seems that F# treats ref a bit differently than C#: Doing some reading on Parameters and Arguments in F# — see the Passing by Reference section — it seems that the ref call copies its argument and uses that copy as the value for the referential cell; so, what I had been passing was a reference cell containing a copy of the contents of msg rather than a reference to the original. The address-of operator (&) gets the address of the value, so that’s what was needed for the arguments to the DefWndProc and WndProc methods instead of wrapping them in refs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.