Using the .NET WebBrowser control, it is fairly simple to execute a member of an HtmlElement.
Assuming there is a JavaScript object called "player" with a member called "getLastSongPlayed"; calling this from the .NET WebBrowser control would go something like this:
HtmlElement elem = webBrowser1.Document.getElementById("player");
elem.InvokeMember("getLastSongPlayed");
Now my question is: How do I acplish that using mshtml ?
Thanks in advance, Aldin
EDIT:
I got it up and running, see my answer below !
Using the .NET WebBrowser control, it is fairly simple to execute a member of an HtmlElement.
Assuming there is a JavaScript object called "player" with a member called "getLastSongPlayed"; calling this from the .NET WebBrowser control would go something like this:
HtmlElement elem = webBrowser1.Document.getElementById("player");
elem.InvokeMember("getLastSongPlayed");
Now my question is: How do I acplish that using mshtml ?
Thanks in advance, Aldin
EDIT:
I got it up and running, see my answer below !
Share Improve this question edited Feb 16, 2012 at 10:07 Aldin asked Feb 14, 2012 at 13:37 AldinAldin 811 silver badge7 bronze badges 5- No matter what I try - It does not work. I can't find anything on the web regarding this issue.. Seems like the only way is to execute the javascript code through executing it through the WebBrowsers address bar.. – Aldin Commented Feb 14, 2012 at 15:30
- so where is the document the webBrowser1 has loaded? – Dai Bok Commented Feb 14, 2012 at 16:08
- what do you mean with "where" it is? – Aldin Commented Feb 14, 2012 at 17:51
- Can you show the html contents of the doc object. Look at this: msdn.microsoft./en-us/library/… how are you creating the html document elements for example: HtmlElement tableElem = doc.CreateElement("TABLE"); – Dai Bok Commented Feb 15, 2012 at 11:56
- I'm not creating any elements. The elements already exist - All I do is reference the element in the document, and I succeed to do so, but I cannot call any underlying script of the element -- the element is an HTML-Element of type "object" and that object is a javascript object that has it's own properties and functions, all I want is to access / call this objects properties / functions. But I cannot.. I can't find a way to do that in MSHTML - The .NET web browser is not an option, I need the InternetExplorer COM object. – Aldin Commented Feb 15, 2012 at 13:26
1 Answer
Reset to default 6FINALLY !! I got it up and running !
The reason for the
System.InvalidCastException
that was thrown, whenever I tried to reference the parentWindow of an mshtml.IHTMLDocument2 and / or assign it to an mshtml.IHTMLWindow2 window object had to do with Threading.
For some, unknown to me, reason it seems that the COM objects of mshtml.IHTMLWindow are operating on another Thread that must be of Single-Threaded Apartment (STA) state.
So the trick was, calling / executing the required piece of code on another Thread with STA state.
Here's a sample code:
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.
}
}
We're now also able to reference the parentWindow of an mshtml.IHTMLDocument2 object and execute a sites script and / or our own (remember it must be on an STA thread):
mshtml.IHTMLWindow2 window = doc.parentWindow;
window.execScript("AScriptFunctionOrOurOwnScriptCode();", "javascript");
This might save someone from headaches in the future. lol