Fatal error on Custom WebBrowser (winforms) code

我们两清 提交于 2019-12-06 17:15:41

问题


Getting a fatal exception on the CustomWebBrowser (winforms) code.

The runtime has encountered a fatal error. The address of the error was at 0x6c9a60c6, on thread 0xf94. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

This is working fine on machines that has .Net Framework 4.5 installed, however not in machines with .Net framework 4.0

// constructor
    public AutoCompleteWebBrowserSite(WebBrowser host) :
        base(host)
    {

        // get the CCW object for this
        _unkOuter = Marshal.GetIUnknownForObject(this);
        Marshal.AddRef(_unkOuter);
        try
        {
            // aggregate the CCW object with the helper Inner object
            _inner = new AutoCompleteHelper(this);
            _unkInnerAggregated = Marshal.CreateAggregatedObject(_unkOuter, _inner);

            // obtain private WebBrowserSite COM interfaces
            try
            {
                _baseIDocHostUiHandler = (WebBrowserNativeMethods.IDocHostUIHandler)  Marshal.GetTypedObjectForIUnknown(_unkInnerAggregated,typeof (WebBrowserNativeMethods.IDocHostUIHandler));
            }
            catch(Exception)
        }
        finally
        {
            Marshal.Release(_unkOuter);
        }
    }

The exception was thrown at _baseIDocHostUiHandler = (WebBrowserNativeMethods.IDocHostUIHandler)Marshal.GetTypedObjectForIUnknown(_unkInnerAggregated,typeof (WebBrowserNativeMethods.IDocHostUIHandler));

Also tried disabling concurrent garbage collection by disabling gcConcurrent in the app.config file

Any help would be really appreciated.


回答1:


Basically need to remove all the code from constructor (mainly _baseIDocHostUiHandler)

And then in the WebBrowserSite implementation, try return the default implementation value for each of the methods.

   private const int DefaultImpVal = unchecked((int)0x80004001)

  #region IDocHostUIHandler
            int WebBrowserNativeMethods.IDocHostUIHandler.ShowContextMenu(int dwId, ref WebBrowserNativeMethods.Point pt, IntPtr pcmdtReserved, IntPtr pdispReserved)
            {
                return DefaultImpVal ;
            }

int WebBrowserNativeMethods.IDocHostUIHandler.ShowUI(int dwId, IntPtr activeObject, IntPtr commandTarget, IntPtr frame, IntPtr doc)
            {
                return DefaultImpVal;
            }

similarly for other methods.



来源:https://stackoverflow.com/questions/28984024/fatal-error-on-custom-webbrowser-winforms-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!