问题
I have a fatal error thrown from my c# code where I have customized forms web browser to support browser autocomplete functionality. The exception gets thrown only on .net 4.0 framework, when we try to obtain the WebBrowserSite COM interfaces.
error thrown at _baseIDocHostUiHandler = (WebBrowserNativeMethods.IDocHostUIHandler) Marshal.GetTypedObjectForIUnknown(_unkInnerAggregated, typeof (WebBrowserNativeMethods.IDocHostUIHandler));
Code:
// 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);
}
}
Any reason and solution for this exception? This does not happen on machines with framework 4.5.
回答1:
Basically need to remove all the code from constructor (mainly _baseIDocHostUiHandler)
And then in the WebBrowserSite implementation, try returning 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/28981784/windows-forms-webbrowser-control-error-on-net-4-0-framework