使用SHDOCVW Navigate2进行Navigatng IE时,操作在Win10中流产



我有一个C#项目,该项目在IE浏览器中使用 Interop.shdocvw

该代码使用Internet ExplorerClass对象的单个实例,并导航到不同的URL。这些URL都在 Intranet Zone中。

该代码在 win7 上正常工作,但在 win10 上存在问题。IE浏览器首次正确打开URL,但是当我尝试使用导航或导航2方法第二次尝试使用它时,我会收到以下错误:

system.runtime.interopservices.comexception(0x80004004):中止操作(Hresult的例外:0x80004004(e_abort)) 在shdocvw.internetexplorerclass.navigate(字符串url,object& flags,object& targetframename,object& postdata,object& headers)

我尝试运行"作为管理员"的主机应用程序,然后运行正常。另外,当我在IE窗口中手动导航到 Internet Zone的URL时,没有问题,但是当手动导航到另一个 intranet url时第二次,错误再次被抛出。

代码

公共静态无效ieopenonurl(字符串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);
    } 
}

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

尝试以下操作:

  SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
  IE.Visible = true;

最新更新