0

I have the following code written in C# WinForms:

...    
SHDocVw.WebBrowser_V1 axBrowser = (SHDocVw.WebBrowser_V1)webBrowser1.ActiveXInstance;
    
if (axBrowser != null)
{
     axBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, axBrowser, new object[] { true });
}
    
axBrowser.NewWindow += axBrowser_NewWindow;
...
if (!webBrowser1.IsDisposed && !webBrowser1.Url.Equals("about:blank")) //System.NullReferenceException: Object reference not set to an instance of an object.
{
     webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
}

I get a null reference error when running on x86 platform. When running on x64, everything is working fine. I suppose the problem is with the Interop.SHDocVw.

Edit: In the if condition, the webBrowser1.Url part becomes null. So i tried like this: (it gives me no error BUT the CONTROL is EMPTY)

private void contextMenuStrip1_Click(object sender, EventArgs e)
{
    try
    {
        if (!webBrowser1.IsDisposed && webBrowser1.Url !=null && !webBrowser1.Url.Equals("about:blank"))
        {
             webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
        }
        else if (webBrowser1.Url == null)
        {
            MessageBox.Show("webBrowser1.Url ");
            webBrowser1.AllowWebBrowserDrop = false;//MS bug?: {"Error HRESULT E_FAIL has been returned from a call to a COM component."}   System.Runtime.InteropServices.COMException
            webBrowser1.ScrollBarsEnabled = false;//false--MS bug?: {"Error HRESULT E_FAIL has been returned from a call to a COM component."}  System.Runtime.InteropServices.COMException
            webBrowser1.ScriptErrorsSuppressed = false;

            WinInetInterop.RestoreSystemProxy();

            SHDocVw.WebBrowser_V1 axBrowser = (SHDocVw.WebBrowser_V1)webBrowser1.ActiveXInstance;

            if (axBrowser != null)
            {
                axBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, axBrowser, new object[] { true });
            }

            axBrowser.NewWindow += axBrowser_NewWindow;

            webBrowser1.Url = new System.Uri("https://evidentacimitir.com/ProfitshareAds.html", System.UriKind.Absolute);
            webBrowser1.WebBrowserShortcutsEnabled = false;
            webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
            webBrowser1.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webBrowser1_Navigated);
        }
    }
    catch (Exception ex)
    {
        ExceptionReportHelper.Exception(ex);
    }
}

How can I fix this problem to work also on x86 platforms?

Thanks

  • I want to know if the webBrower1 in the code is the winform control. If so, I think it is not related to SHDocVw. Because the exception is only related to winform control. If we set the url for webbrowser in the form load, it will not occur the problem – Jack J Jun Jan 11 '21 at 07:23
  • yes, the webbrowser control is a winform control – Molnar Istvan Jan 13 '21 at 08:50
  • 1
    If it is a winform control, it is not related to the Interop.SHDocVw. Because it just check if your webbrowser control's url is about:blank. Is there any other exception in your code? – Jack J Jun Jan 15 '21 at 08:37

0 Answers0