How to get the window handle for the Internet Explorer process you created (LCIE IE 8)
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.
Here is a simple console application you can debug and play with to investigate this technique.
Full listing below (Copy Code):
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:
https://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:
Please drop me a comment if you found this Blog useful!
Comments
Anonymous
April 07, 2010
Very interesting article! Can you also get which TCP ports IE opens as is rendering the page and making calls to load images etc?Anonymous
April 07, 2010
Toni, IE will use the port assigned for the IMG. For example if the page is http://someserver.com/mypage.htm and the image url is a relative url image.jpg then IE sends the request over Port 80. HTTPS will use 443. The image may have a specific port listed such as http://someserver.com/images:8080/image.jpg. You would need to parse the HTML to discover this. You can always use netmon or fiddler http://fiddler2.com to see what the request and response ports are.