How To Get The Window Handle For The Internet Explorer Process You Created Lcie Ie 8
- 04 Sep 2009
One technique people use in accessibility applications is to kick off an application and then grab the Window handle of the process to hook it or do other processing. With loosely coupled Internet Explorer (LCIE) this is a little more difficult that simply getting the handle of the iexplore.exe process you kick off. The reason is that there is a broker IE process and this kicks of the actual IE instance. Look at the links at the end of the article for more information how this works.
The technique you can use is to iterate over the processes and wait for your expected IE instance to get a valid window handle. If there are no other IE instances running the technique is relatively simple.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace LCIE
{
class Program
{
static bool dumpIEWindows(Process [] processes)
{
bool abFoundHandle = false;
if ((processes == null)||(processes.Length == 0))
{
Console.WriteLine(“No IE Processes found”);
}
else
{
foreach (Process nextProcess in processes)
{
if (nextProcess.MainWindowHandle == IntPtr.Zero)
{
Console.Write(“IE process with Window handle set to 0: “ + nextProcess.Id.ToString());
Console.WriteLine(“. This could be the Broker, or the windows is not created yet”);
}
else
{
abFoundHandle = true;
Console.WriteLine(“IE process with handle: “ + nextProcess.Id.ToString() + “. Window Title: “ + nextProcess.MainWindowTitle);
}
}
}
return abFoundHandle;
}
static void Main(string[] args)
{
//TODO: Trap exceptions and take appropriate action if there is an exception
Process[] arrayOfProcesses;
Process aProcess;
// See if there are any iexplore processes already
arrayOfProcesses = System.Diagnostics.Process.GetProcessesByName(“iexplore”);
if (arrayOfProcesses.Length == 0)
{
// create iexplore.exe process
aProcess = Process.Start(“iexplore.exe”);
aProcess.WaitForInputIdle();
arrayOfProcesses = System.Diagnostics.Process.GetProcessesByName(“iexplore”);
}
// TODO: Add some safety valve here to abort the loop after a certain amount of time
// or after a few iterations.
while (dumpIEWindows(arrayOfProcesses) == false)
{
// no IE Windows with a handle so we need to wait for window creation.
// another possibility is that the iexplore broker process has started and it
// did not yet create the brokered process (see LCIE).
// Take a nap…
Thread.Sleep(500);
arrayOfProcesses = System.Diagnostics.Process.GetProcessesByName(“iexplore”);
}
}
}
}
You can extend this example to find the exact window created in the event there are other iexplore.exe processes running before you kick off your process simply by getting a list of PIds (Process Ids) before you create your IExplore.exe process and compare that to the one that you get after you create the process. The process without a NULL window handle will be the correct instance.
**Helpful links**
IE 8 LCIE:
http://blogs.msdn.com/ie/archive/2008/03/11/ie8-and-loosely-coupled-ie-lcie.aspx
Controlling and configuring LCIE:
How to determine which IE tabs goes to which Iexplore.exe process when using Internet Explorer 8: