Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL

1:Winform应用通过mshtml操作IE浏览器DOM时,第一次运行正常,点击第二次时错误信息如下

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in openie01.exe

Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL。

If there is a handler for this exception, the program may be safely continued.

2:出现异常的代码 

为:mshtml.HTMLDocument doc = ie.Document;
SHDocVw.InternetExplorer ie = getInternetExploer(url);
            if (null == ie) 
            {
                //如果没有打开,则进行打开操作,并获取ie对象
                ie = new SHDocVw.InternetExplorer();
                ie.Navigate(url);
                ie.Visible = true;

                ie.DocumentComplete += ie_DocumentComplete;

                compWait();
            }

            //操作DOM进行模拟登陆
            mshtml.HTMLDocument doc = ie.Document;

第一次运行正常,是因为进行了加载完成的判断

private void ie_DocumentComplete(object pDisp, ref object URL)
        {
            ie_Read = true;
        }

        private void compWait()
        {
            while (ie_Read != true)
            {
                Application.DoEvents();
            }
        }

第二次运行异常,是因为ie_Read这个变量没有置位导致的。

3:解决方法

在程序运行完之后将ie_Read置位

 //将标识复位
            ie_Read = false;
原文地址:https://www.cnblogs.com/yshyee/p/7918676.html