title: Strange behaviour of IE and plugin (.Net based)
body: <p>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:</p>
  
  <blockquote>
    <p>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&amp; m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message&amp; m)
    at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
    at System.Windows.Forms.Button.WndProc(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</p>
  </blockquote>
  
  <p>Also in debugging I'm getting the following exception from Visual Studio 2010 just prior the exception above:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I have noticed a very mysterious thing: the exception above occers only in the following cases:</p>
  
  <p>Number of opened tabs-Index of the failed tab (1-based, 0 means nothing)</p>
  
  <p>--------3-----------------------1</p>
  
  <p>--------4-----------------------0</p>
  
  <p>--------5-----------------------2</p>
  
  <p>--------6-----------------------3</p>
  
  <p>--------7-----------------------0</p>
  
  <p>--------8-----------------------5</p>
  
  <p>--------9-----------------------8</p>
  
  <p>--------10----------------------0</p>
  
  <p>--------11----------------------0</p>
  
  <p>--------12----------------------4</p>
  
  <p>I used the following code to open new window:</p>
  
  <p>Plugin.HostInstance.NewWindow(uri, windowName);</p>
  
  <p>NewWindow(...) method uses the following code under the hood:</p>
  
  <pre><code> 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);
          }
  </code></pre>
  
  <p>I tried to use instead of NewWindow the following code:</p>
  
  <pre><code>var psi = new ProcessStartInfo("iexplore", uri);
  Process p = new Process();
  p.StartInfo = psi;
  p.Start();  
  </code></pre>
  
  <p>The error now occured not regulary but time to time. </p>
  
  <p>Please help me to fix or overcome this error.</p>