开发者

Strange behaviour of IE and plugin (.Net based)

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): 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)

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

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:

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

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

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

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

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

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

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

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

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

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

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

I used the following code to open new window:

Plugin.HostInstance.NewWindow(uri, windowName);

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);
        }

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜