如何唯一标识会话0中运行的internet explorer窗口
本文关键字:运行 internet 窗口 explorer 会话 何唯一 唯一 标识 | 更新日期: 2023-09-27 18:24:08
我正在创建自动生成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.Process
强制转换为SHDocVw.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,因此我有以下两个附加步骤(第一个可能适用):C: ''Windows''System32''config''systemprofile创建Desktop文件夹,授予其写入权限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
或者,如果您的用例允许,将WCF应用程序托管在WPF应用程序中,如果需要,您可以在应用程序中托管Internet Explorer窗口,这将为您提供更多的控制。浏览器控件API起初似乎是有限的(智能敏感),直到您将其强制转换为适当的类型。如果需要,我可以发布这个。
编辑:另请查看http://support.microsoft.com/kb/555134