5

I am creating WCF web services that automatea internet explorer. There are multiple web service calls that need to access the same instance of Internet Explorer. However, since the WCF services are hosted on IIS all the calls to the web service are executed in session 0. Now to access the same instance of Internet Explorer I use SHDocVw.InternetExplorer.HWND property which returns the window handle of an Internet Explorer instance. In below code when executed as a WCF service on IIS 7 the window handles always return 0 due to session 0 isolation. Also, I am not able to hook back to the same IE instance or loop through all the open IE windows. I can enumerate the process list and find process ids for each IE window open in session 0, but cannot cast a System.Diagnostics.Process to SHDocVw.InternetExplorer object.

Below is my code:

public int GetWhd()
{
    InternetExplorer ie = new InternetExplorer();
    ie.Visible = true;
    return ie.HWND;
}

public int SetWhd(string whd)
{
    int wh = Int32.Parse(whd);
    InternetExplorer ie = null;
    ShellWindows s = new ShellWindows();
    foreach (SHDocVw.InternetExplorer ie1 in s)
    {
    try
    {
            if (ie1.HWND == wh)
            {
                    ie = ie1;
                    break;
            }
    }
    catch { return 2; }
    }
    if (ie != null) { ie.Navigate("www.google.com"); return 1; }
    return 0;
}

Any help will be much appreciated.

SoftSan
  • 2,482
  • 3
  • 23
  • 54
Neville
  • 63
  • 5
  • 2
    I think the only solution to your problem is to create a satellite process which runs in the same session as Internet Explorer. Then instead of trying to control internet explorer directly from your web service use some inter-process communication mechanism (such as named pipes, WCF or .Net Remoting) to send control commands to your satellite process which then controls ie on behalf of your web service. You should also secure the communication between your web service and your satellite process, especially if your satellite process runs with higher privileges than the logged on user. – Hans Jul 19 '13 at 16:40
  • 2
    Consider using [WebDriver](https://code.google.com/p/selenium/wiki/InternetExplorerDriver) instead. – Aron Jul 19 '13 at 21:07
  • Thanks @Hans .. I ran some tests based on your approach (using named pipes) and it works very well. Thanks for your help! – Neville Jul 22 '13 at 17:59
  • @Aron I already have the procedures developed for individual tasks so it's not feasible for me to rewrite all the code using WebDriver. Also, I'm not sure if using web-driver will help identify the IE window handle, as the issue here is not sHdocVW but it's session 0 isolation. However, I will keep that in mind for future reference.. Thanks.. – Neville Jul 22 '13 at 18:03
  • 1
    @Neville my point is that you would likely not need to uniquely identify an instance of an IE window handle when using webdriver. It has hooks for most automation tasks. – Aron Jul 23 '13 at 03:32

1 Answers1

2

It's difficult to fully escape isolation in IIS7, but here's what I did in a similar scenario: In IIS, go to Advanced Settings on your application pool, and set it to run as a windows user. Make sure you logged in with that user at least once (.net creates some undocumented folders). Set Load User Profile to True

In my case I was automating MS Office, so I had the following 2 additional steps (first might be applicable): C:\Windows\System32\config\systemprofile create Desktop folder, give it write permissions C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Templates make sure Normal.dotm is there and it has write permissions

Next, change settings on the DCOM objects Start -> run -> comexp.msc

Open Component Services -> Computers -> My Computer -> DCOM Config locate the Internet Explorer entry, Right-click -> Properties -> Identity Tab -> select The interactive user

Alternatively, if your use case allows, host the WCF application inside a WPF application, and if needed, you can go as far as hosting an Internet Explorer window inside the app, which will give you more control. The browser control API seems limited at first (intelli-sense wise) until you cast it to the appropriate type. I can post this if needed.

EDIT: Also take a look at http://support.microsoft.com/kb/555134

Alexandru Puiu
  • 741
  • 6
  • 16