IE和插件的奇怪行为(基于.Net) [英] Strange behaviour of IE and plugin (.Net based)

查看:141
本文介绍了IE和插件的奇怪行为(基于.Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpicIE框架和C#4.0编写Internet Explorer插件。我的插件执行以下操作:用户按下按钮(共享链接按钮),插件将打开具有特定URL的单独IE窗口。
当我执行以下操作时:打开3个选项卡,从最后到第一个单击共享链接, - >选项卡#3和#2打开正常窗口。选项卡#1抛出异常:

I'm writing a plugin for Internet Explorer using SpicIE framework and C# 4.0. My plugin does the following: user presses the button (share link button) and the plugin opens separate IE window with specific url. When I do the following: open 3 tabs, from last to first click the 'share link', -> tabs #3 and #2 open window normally. Tab #1 throws an exception:


System.Runtime.InteropServices.COMException(0x80004005):错误HRESULT E_FAIL已从调用返回COM组件。
at SHDocVw.IWebBrowser2.get_Document()
at BGPlugin.Entities.Bookmarklet.ExecuteLinkShareWindow()
at BGPlugin.UserControls.LinkShareControl.btnAdd_Click(Object sender,EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)$系统中的System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)
在系统的System.Windows.Forms.Control.WndProc(消息& m)
。 Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) m)System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在System.Windows.Forms.NativeWindow.Callback的
(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at SHDocVw.IWebBrowser2.get_Document() at BGPlugin.Entities.Bookmarklet.ExecuteLinkShareWindow() at BGPlugin.UserControls.LinkShareControl.btnAdd_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

另外我n调试我在上面的异常之前从Visual Studio 2010中得到以下异常:

Also in debugging I'm getting the following exception from Visual Studio 2010 just prior the exception above:


检测到断开的上下文。
上下文0x2fb4b0已断开连接。不会使用代理来为COM组件提供服务。这可能会导致损坏或数据丢失。为了避免这个问题,请确保所有上下文/公寓都保持活动状态,直到应用程序完全使用代表其中的COM组件的RuntimeCallableWrappers完成。

Disconnected Context was detected. Context 0x2fb4b0 is disconnected. No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

我注意到一件非常神秘的事情:仅在以下情况下才会出现异常:

I have noticed a very mysterious thing: the exception above occers only in the following cases:

打开的标签数量 - 失败标签的索引(1 -based,0表示什么都没有)

Number of opened tabs-Index of the failed tab (1-based, 0 means nothing)

-------- 3 ------------------- ---- 1

--------3-----------------------1

-------- 4 --------------------- - 0

--------4-----------------------0

-------- 5 ----------------------- 2

--------5-----------------------2

-------- 6 ----------------------- 3

--------6-----------------------3

-------- 7 ----------------------- 0

--------7-----------------------0

-------- 8 ----------------------- 5

--------8-----------------------5

-------- 9 ----------------------- 8

--------9-----------------------8

-------- 10 ---------------------- 0

--------10----------------------0

-------- 11 ---------------------- 0

--------11----------------------0

-------- 12 ---------------------- 4

--------12----------------------4

我使用以下代码打开新窗口:

I used the following code to open new window:

Plugin.HostInstance.NewWindow(uri,windowName);

Plugin.HostInstance.NewWindow(uri, windowName);

NewWindow(...)方法使用下面的代码:

NewWindow(...) method uses the following code under the hood:

 try
        {
            object URL = url;
            object flags = System.Reflection.Missing.Value; //special "nothing" value
            object targetName = System.Reflection.Missing.Value; //special "nothing" value
            object postData = System.Reflection.Missing.Value; //special "nothing" value
            object headers = System.Reflection.Missing.Value; //special "nothing" value

            InternetExplorer ie = (InternetExplorer)new InternetExplorer();
            ie.AddressBar = false;
            ie.ToolBar = 0;
            ie.StatusBar = false;
            ie.Width = 480;
            ie.Height = 480;
            ie.Resizable = false;

            ie.Navigate2(ref URL, ref flags, ref targetName, ref postData, ref headers);
            ie.Visible = true;
        }
        catch (Exception ex)
        {
            TraceSink.TraceEvent(TraceEventType.Error, 0, "Exception in Host.NewWindow :" + ex);
        }

我尝试使用以下代码代替NewWindow:

I tried to use instead of NewWindow the following code:

var psi = new ProcessStartInfo("iexplore", uri);
Process p = new Process();
p.StartInfo = psi;
p.Start();  

错误现在不是经常发生,而是时不时发生。

The error now occured not regulary but time to time.

请帮我修复或克服此错误。

Please help me to fix or overcome this error.

推荐答案

IE中的每个选项卡都在自己的线程上打开,自带BHO / Toolbar等实例。每个3个或更多选项卡在自己的进程中启动。因此,仅使用一个静态Plugin.HostInstance是完全错误的。
我的第一个解决方法是使用静态字典填充OnDocumentComplete,但是在访问浏览器文档属性时我经常收到UnauthorizedAccessException,所以我想正确的模式是使用来自IObjectWithSite COM接口的GetSite方法。

Each Tab in IE is opened on own thread with own instance of BHO/Toolbar etc. Every 3 or more tabs launched in own process. So using only one static Plugin.HostInstance is completely incorrect. My first workaround was having static Dictionary which was filled OnDocumentComplete, but I have frequently receiving UnauthorizedAccessExceptionwhen accessing to browser document properties, so I guess the right pattern is using GetSite method from IObjectWithSite COM interface.

这篇关于IE和插件的奇怪行为(基于.Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆