我正在創建WCF web services
該自動化程序internet explorer
。有多個Web服務調用需要訪問Internet Explorer
的同一個實例。但是,由於WCF
服務託管在IIS
上,所有對Web服務的調用都在會話0中執行。現在要訪問同一個Internet Explorer
實例,我使用SHDocVw.InternetExplorer.HWND
屬性,該屬性返回Internet Explorer
實例的窗口句柄。在下面的代碼中,當作爲WCF
服務在IIS 7
上執行時,由於會話0隔離,窗口句柄總是返回0。此外,我無法回到相同的IE
實例或通過所有打開的IE
窗口循環。我可以枚舉進程列表並找到在會話0中打開的每個IE
窗口的進程標識,但不能將System.Diagnostics.Process
轉換爲SHDocVw.InternetExplorer
對象。如何唯一標識在會話0中運行的Internet Explorer窗口?
下面是我的代碼:
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;
}
任何幫助將非常感激。
我覺得你的問題的唯一解決方案是創建運行在相同的會話與Internet Explorer衛星的過程。然後,不要試圖直接從Web服務控制Internet Explorer,而是使用一些進程間通信機制(如命名管道,WCF或.Net Remoting)將控制命令發送到您的衛星進程,然後控制命令即代表您的Web服務。您還應該確保您的Web服務和衛星進程之間的通信安全,特別是如果您的衛星進程以高於登錄用戶的特權運行。 – Hans
請考慮使用[WebDriver](https://code.google.com/p/selenium/wiki/InternetExplorerDriver)。 – Aron
謝謝@Hans ..我根據你的方法(使用命名管道)運行了一些測試,它工作得很好。謝謝你的幫助! – Neville