如何唯一标识在会话0中运行的Internet Explorer窗口?

我正在创建自动化internet explorer WCF web services 。 有多个Web服务调用需要访问Internet Explorer的同一实例。 但是,由于WCF服务托管在IIS所有对Web服务的调用都在会话0中执行。现在要访问Internet Explorer的同一实例,我使用SHDocVw.InternetExplorer.HWND属性返回Internet Explorer实例的窗口句柄。 在下面的代码中,当作为IIS 7上的WCF服务执行时,由于会话0隔离,窗口句柄始终返回0。 此外,我无法挂钩到相同的IE实例或循环所有打开的IE窗口。 我可以枚举进程列表并查找在会话0中打开的每个IE窗口的进程ID,但不能将System.Diagnostics.ProcessSHDocVw.InternetExplorer对象。

以下是我的代码:

 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; } 

任何帮助都感激不尽。

在IIS7中完全逃避隔离是很困难的,但这是我在类似场景中所做的:在IIS中,转到应用程序池上的“高级设置”,并将其设置为以Windows用户身份运行。 确保您至少使用该用户登录一次(.net创建一些未记录的文件夹)。 将加载用户配置文件设置为True

在我的情况下,我是自动化MS Office,所以我有以下2个额外的步骤(第一个可能适用):C:\ Windows \ System32 \ config \ systemprofile创建桌面文件夹,给它写权限C:\ Windows \ System32 \ config \ systemprofile \ AppData \ Roaming \ Microsoft \ Templates确保Normal.dotm存在,并且具有写入权限

接下来,更改DCOM对象上的设置Start -> run -> comexp.msc

打开Component Services -> Computers -> My Computer -> DCOM Config找到Internet Explorer条目, Right-click -> Properties -> Identity Tab -> select The interactive user

或者,如果您的用例允许,在WPF应用程序中托管WCF应用程序,并且如果需要,您可以在应用程序内部托管Internet Explorer窗口,这将为您提供更多控制。 浏览器控件API最初似乎是有限的(智能方面),直到您将其转换为适当的类型。 如果需要,我可以发布。

编辑:另请参阅http://support.microsoft.com/kb/555134