当使用SHDocVw Navigate2进行导航时,操作中止了WIN10中的异常

时间:2016-06-15 09:52:01

标签: c# internet-explorer iwebbrowser2 shdocvw

我有一个C#项目,使用 Interop.SHDocVw 在IE浏览器中打开网址。

代码使用InternetExplorerClass对象的单个实例并导航到不同的URL。这些网址都位于 Intranet 区域。

代码在 WIN7 上运行良好,但在 WIN10 时出现问题。 IE浏览器在第一次正确打开网址,但是当我第二次使用navigate或navigate2方法尝试使用它时,我收到以下错误:

System.Runtime.InteropServices.COMException(0x80004004):操作已中止(来自HRESULT的异常:0x80004004(E_ABORT))    在SHDocVw.InternetExplorerClass.Navigate(String URL,Object& Flags,Object& TargetFrameName,Object& PostData,Object& Headers)

我尝试以管理员身份运行主机应用程序"然后它工作正常。 此外,当我在IE窗口中手动导航到互联网区域中的网址时,没有问题,但是当手动导航到其他 Intranet 网址并尝试运行代码时第二次,错误再次被抛出。

代码

public static void IEOpenOnURL(string url)         {             对象o = null;             bool newBrowser = false;

        //check if window is already opened 
        if(webBrowser==null)
        {
            newBrowser = true;
        }
        else
        {               
            try 
            {
                //Cannot navigate in browser with an open modal dialog
                if(webBrowser.Busy)
                {
                    MessageBox.Show("הדפדפן תפוס - סגור את החלונות הקופצים מתוך הדפדפן הפתוח");
                    return;
                }
            }
            //an error is thrown when the browser was closed by user
            catch(Exception)
            {
                newBrowser = true;
            }
        } 

        if(newBrowser)
        { 
            //create new instance of the browser
            InternetExplorer explorer = new InternetExplorerClass(); 
            webBrowser = (IWebBrowser2)explorer;  
        } 


        webBrowser.Visible = true; 
        //webBrowser.Navigate(url, ref o, ref o, ref o, ref o);
        webBrowser.Navigate2(url, ref o, ref o, ref o, ref o);

        //Set focus 
        SetForegroundWindow((IntPtr)webBrowser.HWND);
        //If browser is minimized - show it
        ShowWindow((IntPtr)webBrowser.HWND,9);
    } 
}

1 个答案:

答案 0 :(得分:0)

我不会说我知道确切的答案。但我会说你通常应该通过界面而不是类来初始化Internet Explorer。当我尝试直接初始化类时,我遇到了错误。

试试这个:

  SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
  IE.Visible = true;
相关问题