问题描述
我有我的C#code,我已经定制形式的网页浏览器,支持浏览器的自动完成功能,抛出一个致命的错误。该异常被抛出只能在.NET 4.0框架,当我们试图获得WebBrowserSite COM接口。
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:
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);
}
}
有关此异常任何原因及解决办法?这不会发生在机器的框架4.5。
Any reason and solution for this exception? This does not happen on machines with framework 4.5.
推荐答案
基本上需要从构造函数(主要是_baseIDocHostUiHandler)删除所有code
Basically need to remove all the code from constructor (mainly _baseIDocHostUiHandler)
和然后在WebBrowserSite实现中,尝试返回默认实现值的每个的方法。
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.
这篇关于Windows窗体在.NET 4.0框架WebBrowser控件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!