如何强制Internet Explorer中打开它的当前运行的实例,而不是创建一个新的?(How t

2019-07-28 22:54发布

如何以编程方式启动IE浏览器( iexplore.exe ),并在当前正在运行的实例导航(通过打开一个新标签,或替换当前URL)而不是创建一个新的实例。

我已经寻找一个命令行开关,并尝试使用InternetExplorer.Application ,无济于事。

下面是我需要的东西(IE6-IE9将是很好)

ShellExecute(Handle, 'open', 'iexplore.exe',
    '"http://google.com" -single_instance',
    nil, SW_RESTORE);

下面是一些代码来证明我的尝试。 在Delphi代码(部分基于如何从HWND的IHTMLDocument2获取 ):

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 <> 0 then
  try
    @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
    if @ObjectFromLresult <> nil then
    begin
      Msg := RegisterWindowMessage('WM_HTML_GETOBJECT');
      if SendMessageTimeOut(WHandle, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes) <> 0 then
        if ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc) = S_OK then
        begin
          (pDoc.parentWindow as IServiceprovider).QueryService(
            IWebbrowserApp, IWebbrowser2, IE);
          Result := IE <> 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 <> 0 then
  begin
    WndChild := FindWindowEx(Wnd, 0, 'Shell DocObject View', nil);
    if WndChild <> 0 then
    begin
      WndChild := FindWindowEx(WndChild, 0, 'Internet Explorer_Server', nil);
      if WndChild <> 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 <> 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;

Answer 1:

你的方法“1”的实际工作。 这个问题,至少在这里,是“壳牌DocObject的视图”窗口不是顶级IE窗口的直接子。 这里用IE8,“互联网Explorer_Server”窗口的“壳牌DocObject的观”,这是“TabWindowClass”,这是“帧标签”的孩子的孩子一个孩子。 如果你能在方法1确认“FindWindowEx”返回0,这就是为什么它失败。 下面的是修改为使用代码EnumChildWindows

function EnumChilds(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
const
  Server = 'Internet Explorer_Server';
var
  ClassName: array[0..24] of Char;
begin
  GetClassName(hwnd, ClassName, Length(ClassName));
  Result := ClassName <> Server;
  if not Result then
    PLongWord(lParam)^ := hwnd;
end;

function GetActiveIEServerWindow(const Activate: Boolean=True): HWND;
var
  Wnd, WndChild: HWND;
begin
  Result := 0;
  Wnd := FindWindow('IEFrame', nil); // top level IE
  if Wnd <> 0 then
  begin

//    WndChild := FindWindowEx(Wnd, 0, 'Shell DocObject View', nil);
//    if WndChild <> 0 then
//    begin
//      WndChild := FindWindowEx(WndChild, 0, 'Internet Explorer_Server', nil);

    WndChild := 0;
    EnumChildWindows(Wnd, @EnumChilds, LongWord(@WndChild));

    if WndChild <> 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;

至于方法“2”,我已经看到了说明IE不支持恢复其活动对象的引用网络上的一些环节,但我没有对任何官方参考。



文章来源: How to force Internet Explorer to open it's current running instance rather than creating a new one?