Replace WinForms webbrowser error message

0

I am using WinForms web browser and I would like to display a custom error html when the page is not valid but I can't figure out how to do it. I have this little snippet for displaying it:

        if (wb_Main.Document != null)
        { wb_Main.Document.Write(string.Empty); }
        wb_Main.DocumentText = html;

And I found some code on SO to wrap the WebBrowser in order to get to the navigate error event which seems to be working fine. However whenever I try to set the html in the error event I get an exception:

 System.Runtime.InteropServices.COMException: The requested resource is in use. (Exception from HRESULT: 0x800700AA)

With stack trace:

   at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.set_Url(Uri value)
   at System.Windows.Forms.WebBrowser.set_DocumentStream(Stream value)
   at System.Windows.Forms.WebBrowser.set_DocumentText(String value)
   at RDES.CMI.Revit.CADDBrowser.UC_Browser.wb_Main_NavigateError(Object sender, WebBrowserNavigateErrorEventArgs e) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\UC_Browser.xaml.cs:line 84
   at RDES.CMI.Revit.CADDBrowser.WebBrowser2.OnNavigateError(WebBrowserNavigateErrorEventArgs e) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\WebBrowser2.cs:line 49
   at RDES.CMI.Revit.CADDBrowser.WebBrowser2.WebBrowser2EventHelper.NavigateError(Object pDisp, Object& url, Object& frame, Object& statusCode, Boolean& cancel) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\WebBrowser2.cs:line 69

I even tried to set an error text variable in the navigate error handler and then wait for the document completed event to re-navigate to the error text but document completed never fires. The only event I got to fire after the navigate error is the 'status text changed' event which throws the same error when I try to display the error.

I had originally used the WPF browser control which did this very well, but unfortunately the program I'm plugging into seems to have a major issue with the WPF browser (it crashes the program under certain relatively common circumstances).

Is there a way to do this? I'm feeling a bit stuck...

Here is the code of the wrapper class that I found here in case it's needed:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RDES.CMI.Revit.CADDBrowser
{
    public class WebBrowser2 : WebBrowser
    {
        AxHost.ConnectionPointCookie cookie;
        WebBrowser2EventHelper helper;

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();

            // Create an instance of the client that will handle the event
            // and associate it with the underlying ActiveX control.
            helper = new WebBrowser2EventHelper(this);
            cookie = new AxHost.ConnectionPointCookie(
                this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void DetachSink()
        {
            // Disconnect the client that handles the event
            // from the underlying ActiveX control.
            if (cookie != null)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNavigateErrorEventHandler NavigateError;

        // Raises the NavigateError event.
        protected virtual void OnNavigateError(
            WebBrowserNavigateErrorEventArgs e)
        {
            if (this.NavigateError != null)
            {
                this.NavigateError(this, e);
            }
        }

        // Handles the NavigateError event from the underlying ActiveX 
        // control by raising the NavigateError event defined in this class.
        private class WebBrowser2EventHelper :
            StandardOleMarshalObject, DWebBrowserEvents2
        {
            private WebBrowser2 parent;

            public WebBrowser2EventHelper(WebBrowser2 parent)
            {
                this.parent = parent;
            }

            public void NavigateError(object pDisp, ref object url,
                ref object frame, ref object statusCode, ref bool cancel)
            {
                // Raise the NavigateError event.
                this.parent.OnNavigateError(
                    new WebBrowserNavigateErrorEventArgs(
                    (String)url, (String)frame, (Int32)statusCode, cancel));
            }
        }
    }

    // Represents the method that will handle the WebBrowser2.NavigateError event.
    public delegate void WebBrowserNavigateErrorEventHandler(object sender,
        WebBrowserNavigateErrorEventArgs e);

    // Provides data for the WebBrowser2.NavigateError event.
    public class WebBrowserNavigateErrorEventArgs : EventArgs
    {
        private String urlValue;
        private String frameValue;
        private Int32 statusCodeValue;
        private Boolean cancelValue;

        public WebBrowserNavigateErrorEventArgs(
            String url, String frame, Int32 statusCode, Boolean cancel)
        {
            urlValue = url;
            frameValue = frame;
            statusCodeValue = statusCode;
            cancelValue = cancel;
        }

        public String Url
        {
            get { return urlValue; }
            set { urlValue = value; }
        }

        public String Frame
        {
            get { return frameValue; }
            set { frameValue = value; }
        }

        public Int32 StatusCode
        {
            get { return statusCodeValue; }
            set { statusCodeValue = value; }
        }

        public Boolean Cancel
        {
            get { return cancelValue; }
            set { cancelValue = value; }
        }
    }

    // Imports the NavigateError method from the OLE DWebBrowserEvents2 
    // interface. 
    [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    TypeLibType(TypeLibTypeFlags.FHidden)]
    public interface DWebBrowserEvents2
    {
        [DispId(271)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame,
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }
}
c#
winforms
webbrowser-control
asked on Stack Overflow Sep 7, 2016 by sfaust • edited Sep 7, 2016 by sfaust

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0