5

When accessing Process.MainWindowTitle as follows...

Process[] processes = Process.GetProcessesByName( "iexplore" );

...and then loop over the resulting array, I always end up with the MainWindowTitle being empty for all but one item in the array. In my case I've got two Internet Explorer windows open, one with a single tab and one with two tabs in it.

Running my code I always get the MainWindowTitle for the window and tab that I had last active - all the others remain empty. The strange thing is that the process ID in which the MainWindowTitle is filled is always the same - if I activate the other IE window or tab before running my code, the process ID is always the same:

  if ( !processes.Any() )
  {
    MessageBox.Show( "TODO - No matching process found" );
    return;
  }

  if ( processes.Count() > 1 )
  {
    foreach ( Process currentProcess in processes )
    {
      // More than one matching process found
      checkedListBox1.Items.Add( currentProcess.Id + " - " + currentProcess.MainWindowTitle + " - " + currentProcess.ProcessName );
    }

    return;
  }

Output could therefore be for the first run like:

  • 4824 - - iexplore
  • 3208 - - iexplore
  • 4864 - Google - Windows Internet Explorer - iexplore

Next run (with the other IE window selected beforehand):

  • 4824 - - iexplore
  • 3208 - - iexplore
  • 4864 - id Software - Windows Internet Explorer - iexplore

I've read about this post, but I didn't get any further with my problem (and it seems to go into a somewhat different direction anyway).

Why do I always only get one non-empty MainWindowTitle?

Community
  • 1
  • 1
Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
  • what are you trying to achieve ? – Yahia Apr 08 '12 at 15:26
  • I'd like to inform the user if more than one Internet Explorer window is open (by listing the windows with their respective title). The reason behind this, again, is to be able to automatically create a screenshot of the Internet Explorer window (which is going to be used to test web applications and, in case of an error, process this information). – Gorgsenegger Apr 08 '12 at 15:31
  • I'm not sure if this might help (let me know and I can elaborate in the answer) but you can use that to your advantage. i.e. with around IE (and mostly any 'automation' of that sort) you need to use some 'ad hoc' tricks. E.g. in your case you could check for `.MainWindowHandle` of the process and the title you're already using - and if empty it's tab, if you get more info you picked yourself a real window, or close to that. Problem is that that thing changes with IE versions, so you'd have to adjust it. – NSGaga-mostly-inactive Apr 08 '12 at 16:04
  • I think I might get away with "just" showing the "real" IE windows and their active tab (if more than one exists), although this isn't a 100% solution and a bit of a dirty workaround. It also looks as if only the one process containing the MainWindowTitle has a MainWindowHandle. – Gorgsenegger Apr 08 '12 at 16:15
  • @Gorgsenegger any reason you are not using the COM interface of IE (either the .NET WebBrowser control or some other wrapper) ? – Yahia Apr 08 '12 at 16:31
  • @Yahia - Wouldn't I have to use that if I wanted to provide the "browsing" within my app? I'm afraid I won't be allowed to do that as they web application shall be tested on the IE "platform" as it is going to be used like that by the customers. Or did I misunderstand your comment? – Gorgsenegger Apr 08 '12 at 16:35
  • @Gorgsenegger what you want is basically NOT possible in a robust manner without some kind of direct browser access... you either use a .NET WebBrowser control or implement a BHO which is loaded once per Tab and gets notified by IE about almost everything going on in that Tab... – Yahia Apr 08 '12 at 16:37
  • @Yahia - Thanks for that piece of information, now I know that I won't have to spend more time going down that path. Might have to amend our approach in light of that. – Gorgsenegger Apr 08 '12 at 16:39
  • @Gorgsenegger you are welcome :-) please see my answer below with links about BHOs/relevant event etc. HTH – Yahia Apr 08 '12 at 16:46
  • I am waiting for @Hans Passant to answer this! – nawfal Apr 08 '12 at 18:04
  • @Yahia - Thanks again, I'll look into it within the next couple of days and then update this page - have to get back to Easter now, otherwise I'll be in trouble ;-) – Gorgsenegger Apr 08 '12 at 21:55

3 Answers3

3

Remember that internet explorer uses a hosting model - one iexplore.exe instance hosts the internet explorer frame, the other iexplore.exe instances just display the contents of tabs.

The only IE instance with a top level window is the iexplore.exe process which hosts the frame.

This article discusses the multi-process architecture of various web browsers. As I understand it, browsers are moving to a multi-process model - that way a failure in one web page won't affect other pages. This article goes into more detail about IE's multi-process model.

Larry Osterman
  • 16,086
  • 32
  • 60
1

One option to make this happen reliabely (see comments above) is to implement a BHO - esp. the DWebBrowserEvents2::WindowStateChanged Event is usefull in this scenario.

BHOs are implementations of a bunch of COM interfaces and get loaded into the browser process/each tab... best way to implement them is in C++ IMO.

But it is certainly possible to do so in .NET (although not recommended):

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

I had the same problem. With Teamviewer Host, we have the QuickConnect Button on some Application. In my case WinWord. If you remove or disable the QuickConnect, the MainWindowTitle will be enhanced

Kriba
  • 1