ConsoleアプリケーションでSHDocVwでInternetExplorerを動かす
ConsoleアプリケーションでComを利用したアプリケーションを作成する場合、Mainで直接処理を記述した場合、DocumentCompleteEventが発火しない。
そのため、Runメソッドなどを用意し、Programクラスのインスタンスを作り、かつイベント登録用の別メソッドを用意してやる必要がある。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using SHDocVw; namespace ConsoleApplication5 { class Program { public SHDocVw.InternetExplorer ie = null; static private IWebBrowserApp ieb = null; static void Main(string[] args) { Program program = new Program(); program.Run(); } public void Run() { ie = new SHDocVw.InternetExplorer(); ieb = (IWebBrowserApp)ie; setAllEvent(); ieb.Visible = true; ieb.Navigate("about:blank"); ((mshtml.IHTMLDocument2)ieb.Document).body.innerHTML = @"<iframe src="" https://www.amazon.jp/reviews/iframe? akid=**********& alinkCode=xm2& asin=B00V7Q9IX6& atag=bonkagu0a-22& exp=2017-03-16T00%3A03%3A30Z& v=2& sig=*************D "" width=""1000"" height=""1000"">〜</iframe>"; System.Threading.Thread.Sleep(1000); ieb.Navigate("http://www.yahoo.co.jp"); } void setAllEvent() { ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.ie_DocumentComplete); } /// <summary> /// /// </summary> /// <param name="pDisp"></param> /// <param name="URL"></param> public void ie_DocumentComplete(object pDisp, ref object URL) { IWebBrowser2 iwb = (IWebBrowser2)pDisp; mshtml.IHTMLDocument2 hi = (mshtml.IHTMLDocument2)iwb.Document; string html = hi.body.innerHTML; //throw new NotImplementedException(); } } }