Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Process.MainWindowTitle always empty for all but one window?

Tags:

c#

.net

windows

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?

like image 200
Gorgsenegger Avatar asked Oct 08 '22 04:10

Gorgsenegger


2 Answers

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.

like image 127
ReinstateMonica Larry Osterman Avatar answered Oct 13 '22 00:10

ReinstateMonica Larry Osterman


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):

  • http://www.codeproject.com/Articles/350432/BHO-Development-using-managed-code
  • http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c19613/Build-a-Managed-BHO-and-Plug-into-the-Browser.htm
  • AddIn-Express for IE (commercial library for .NET-based BHOs)
like image 25
Yahia Avatar answered Oct 13 '22 01:10

Yahia