最後に!!私はそれを稼いだと実行している!
私はmshtml.IHTMLDocument2のparentWindowを参照および/またはmshtml.IHTMLWindow2ウィンドウオブジェクトに代入しようとしたときに、スローされた
System.InvalidCastException
理由は、スレッドとしなければなりませんでした。
mshtml.IHTMLWindowのCOMオブジェクトがシングルスレッドアパートメント(STA)状態でなければならない別のスレッドで動作しているように見える理由があります。
だから、トリックは、STA状態の別のスレッドで必要なコードを呼び出して実行していました。
は、ここでのサンプルコードです:
SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer
bool _isRunning = false;
private void IE_DocumentComplete(object pDisp, ref obj URL)
{
//Prevent multiple Thread creations because the DocumentComplete event fires for each frame in an HTML-Document
if (_isRunning) { return; }
_isRunning = true;
Thread t = new Thread(new ThreadStart(Do))
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void Do()
{
mshtml.IHTMLDocument3 doc = this.IE.Document;
mshtml.IHTMLElement player = doc.getElementById("player");
if (player != null)
{
//Now we're able to call the objects properties, function (members)
object value = player.GetType().InvokeMember("getLastSongPlayed", System.Reflection.BindingFlags.InvokeMethod, null, player, null);
//Do something with the returned value in the "value" object above.
}
}
我々はまた、今mshtml.IHTMLDocument2オブジェクトのparentWindowを参照すると、サイトのスクリプトおよび/または私たち自身を実行することができます(覚えて、それはSTAである必要がありますスレッド):
mshtml.IHTMLWindow2 window = doc.parentWindow;
window.execScript("AScriptFunctionOrOurOwnScriptCode();", "javascript");
これにより、将来の頭痛から誰かを救うことができます。 lol
私が何を試しても - それは動作しません。私はこの問題に関してウェブ上で何も見つけることができません。唯一の方法はWebBrowsersのアドレスバーを通して実行することによってJavaScriptコードを実行することです。 – Aldin
webBrowser1がロードした文書はどこにありますか? –
「どこ?」とはどういう意味ですか? – Aldin