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

Nev*_*lle 5 c# wcf session-0-isolation internet-explorer-8

我正在创建WCF web services自动化internet explorer.有多个Web服务调用需要访问同一个实例Internet Explorer.但是,由于WCF服务托管在IISWeb服务的所有调用都在会话0中执行.现在访问Internet ExplorerI use 的同一个实例,SHDocVw.InternetExplorer.HWND它返回Internet Explorer实例的窗口句柄.在下面的代码中,作为窗口WCFIIS 7的服务执行句柄时,由于会话0隔离,总是返回0.此外,我无法挂钩到同一个IE实例或循环所有打开的IE窗口.我可以枚举进程列表并查找IE在会话0中打开的每个窗口的进程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;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽.

Ale*_*uiu 2

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

就我而言,我正在自动化 MS Office,因此我有以下 2 个附加步骤(第一个可能适用): 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

或者,如果您的用例允许,可以在 WPF 应用程序内托管 WCF 应用程序,并且如果需要,您甚至可以在应用程序内托管 Internet Explorer 窗口,这将为您提供更多控制权。浏览器控制 API 起初似乎受到限制(智能感知),直到您将其转换为适当的类型。如果需要的话我可以发布这个。

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