title: How to force Internet Explorer to open it's current running instance rather than creating a new one?
body: <p>How to programatically launch IE (<code>iexplore.exe</code>) and navigate in the current running instance (by opening a new tab, or replacing the current URL) Rather than creating a new instance.</p>
  
  <p>I have searched for a command line switch, and tried using <code>InternetExplorer.Application</code>, to no avail. </p>
  
  <p>Here is a <strong>pseudo</strong> of what I need (IE6-IE9 would be nice):</p>
  
  <pre><code>ShellExecute(Handle, 'open', 'iexplore.exe',
      '"http://google.com" -single_instance',
      nil, SW_RESTORE);
  </code></pre>
  
  <hr>
  
  <p>Here is some code to demonstrate my attempts. code in Delphi (partly based on <a href="http://support.microsoft.com/kb/249232" rel="nofollow">How to get IHTMLDocument2 from a HWND</a>):</p>
  
  <pre><code>implementation
  
  uses ShellApi, ComObj, ActiveX, SHDocVw, MSHTML;        
  
  function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): Boolean;
  type
    TObjectFromLResult = function(LRESULT: lResult; const IID: TIID;
      wParam: WPARAM; out pObject): HRESULT; stdcall;
  var
    hInst: HMODULE;
    lRes: Cardinal;
    Msg: UINT;
    pDoc: IHTMLDocument2;
    ObjectFromLresult: TObjectFromLresult;
  begin
    Result := False;
    hInst := LoadLibrary('oleacc.dll');
    if hInst &lt;&gt; 0 then
    try
      @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
      if @ObjectFromLresult &lt;&gt; nil then
      begin
        Msg := RegisterWindowMessage('WM_HTML_GETOBJECT');
        if SendMessageTimeOut(WHandle, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes) &lt;&gt; 0 then
          if ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc) = S_OK then
          begin
            (pDoc.parentWindow as IServiceprovider).QueryService(
              IWebbrowserApp, IWebbrowser2, IE);
            Result := IE &lt;&gt; nil;
          end;
        end;
    finally
      FreeLibrary(hInst);
    end;
  end;
  
  function GetActiveIEServerWindow(const Activate: Boolean=True): HWND;
  var
    Wnd, WndChild: HWND;
  begin
    Result := 0;
    Wnd := FindWindow('IEFrame', nil); // top level IE
    if Wnd &lt;&gt; 0 then
    begin
      WndChild := FindWindowEx(Wnd, 0, 'Shell DocObject View', nil);
      if WndChild &lt;&gt; 0 then
      begin
        WndChild := FindWindowEx(WndChild, 0, 'Internet Explorer_Server', nil);
        if WndChild &lt;&gt; 0 then
        begin
          Result := WndChild;
          if Activate then
          begin
            if IsIconic(Wnd) then
              ShowWindow(Wnd, SW_RESTORE)
            else
              SetForegroundWindow(Wnd);
          end;
        end;
      end;
    end;
  end;
  
  // Method 1
  procedure TForm1.Button1Click(Sender: TObject);
  const
    navOpenInNewTab = $800;
  var
    IEServerWnd: HWND;
    IE: IWebBrowser2;
  begin
    IEServerWnd := GetActiveIEServerWindow;
    if (IEServerWnd &lt;&gt; 0) and GetIEFromHWnd(IEServerWnd, IE) then
    begin
      // *** this opens the Default browser, e.g Google Chrome 
      // *** if IE is the Default browser, an empty new window is opened.
      OleVariant(IE).Navigate('http://www.yahoo.com', Longint(navOpenInNewTab));
    end
    else
    begin
      ShellExecute(Handle, 'open', 'iexplore.exe',
      '"http://google.com"',
      nil, SW_RESTORE);
    end;
  end;
  
  procedure InternetExplorerNavigate(URL: WideString);
  const
    navOpenInNewTab = $800;
  var
    IE: OleVariant;
  begin
    try
      // *** this always fails (security constraints?)
      IE := GetActiveOleObject('InternetExplorer.Application');
    except
      IE := CreateOleObject('InternetExplorer.Application');
    end;
    IE.Visible := True;
    IE.Navigate(URL, Longint(navOpenInNewTab));
  end;
  
  // Method 2
  procedure TForm1.Button2Click(Sender: TObject);
  begin
    InternetExplorerNavigate('http://google.com');
  end;
  </code></pre>