I am writing a BHO for IE11. In the BHO I have exposed some methods using IExtension interface to be called by the JavaScript. The Code is below
IWebBrowser2 _webBrowser2;
int IObjectWithSite.SetSite(object site)
{
try
{
_site = site;
if (site != null)
{
var serviceProv = (IServiceProvider)_site;
var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp));
var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2));
IntPtr intPtr;
serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);
_webBrowser2 = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);
((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute += S2_BeforeScriptExecute;
}
else
{
((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute -= S2_BeforeScriptExecute;
_webBrowser2 = null;
}
return 0;
}
private void S2_BeforeScriptExecute(object pDispWindow)
{
dynamic window = _webBrowser2.Document.parentWindow;
IExpando windowEx = (IExpando)window;
windowEx.AddProperty("myExtension");
window.myExtension = this;
}
In this everything is working fine except that when user clicks on refresh button in IE, _webBrowser2.Document.parentWindow gives exception
System.NotSupportedException: Exception from HRESULT: 0x800A01B6
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at CallSite.Target(Closure , CallSite , ComObject )
at CallSite.Target(Closure , CallSite , Object )
at BHO.S2_BeforeScriptExecute(Object pDispWindow)
Also I have tried instance of SHDocVw.WebBrowser instead of IWebBrowser2, but still the same issue.
So is there any alternative to this or something else that works fine?