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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM