We help IT Professionals succeed at work.

how to distinguish instances of Internet Explorer from Windows Explorer in CoShellWindows.Create list?

KKostya
KKostya asked
on
how to distinguish instances of Internet Explorer from Windows Explorer in CoShellWindows.Create list?
I can enumerate all Shell instances, but only when accessing Internet Explorer methods, Windows Explorer instances throw exception "No such interface supported"

 ShellWindow := CoShellWindows.Create;
  if ShellWindow.Count>0 then for iShellWin:=0 to ShellWindow.Count-1 do
  begin
    ShellWinItem := ShellWindow.Item(iShellWin);
. . .
Comment
Watch Question

Top Expert 2010
Commented:
Hi KKostya. Try this code:
uses
  ComObj, // for CoInitialize/CoUninitialize
  SHDocVw;

function GetWindowClassName(hWindow: THandle): string;
begin
  SetLength(Result, 250);
  SetLength(Result, GetClassName(hWindow, PChar(Result), 248));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  ShellWindow: IShellWindows;
  WebBrowser: IWebBrowser2;
  WndClassName: string;
  URL: OleVariant;
  Flags: OleVariant;
  TargetFrameName: OleVariant;
  PostData: OleVariant;
  Headers: OleVariant;
begin
  ShellWindow:= CoShellWindows.Create;
  for i:=0 to ShellWindow.Count-1 do
  begin
    if ShellWindow.Item(i).QueryInterface( IWebBrowser2, WebBrowser) = S_OK then
    begin
      WndClassName:= GetWindowClassName(WebBrowser.HWND);
      if WndClassName = 'ExploreWClass' then
      begin
        // Item(i) is instance of Window Explorer
        // it still supports IWebBrowser2 interfaces
        ShowMessage( 'It''s Window Explorer: ' + WebBrowser.LocationURL );
        // we can use WebBrowser as IWebBrowser2 interface
        // For example:
        URL:= 'http:\\google.com';
        Flags:= 0;
        TargetFrameName:= '';
        PostData:= '';
        Headers:= '';
        WebBrowser.Navigate2( URL, Flags, TargetFrameName, PostData, Headers);
      end
      else if WndClassName = 'IEFrame' then
      begin
        // Item(i) is instance of Internet Explorer
        ShowMessage( 'It''s Internet Explorer: ' + WebBrowser.LocationURL );
        // we can use WebBrowser as IWebBrowser2 interface
        // For example:
        URL:= 'http:\\google.com';
        Flags:= 0;
        TargetFrameName:= '';
        PostData:= '';
        Headers:= '';
        WebBrowser.Navigate2( URL, Flags, TargetFrameName, PostData, Headers);
      end;
    end
    else
      // Item(i) is instance of Window Explorer
      ShowMessage( 'Window Explorer, but doesn''t support IWebBrowser' );
  end;
end;

Open in new window

Author

Commented:
it works great