MSHTML:调用 javascript 对象的成员?
使用 .NET WebBrowser 控件,执行 HtmlElement 的成员相当简单。
假设有一个名为“player”的 JavaScript 对象,其成员名为“getLastSongPlayed”;从 .NET WebBrowser 控件调用它会像这样:
HtmlElement elem = webBrowser1.Document.getElementById("player");
elem.InvokeMember("getLastSongPlayed");
现在我的问题是:如何使用 mshtml 来实现这一点?
提前致谢, 奥尔丁
编辑:
我已经启动并运行了它,请参阅下面的答案!
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 accomplish that using mshtml ?
Thanks in advance,
Aldin
EDIT:
I got it up and running, see my answer below !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后 !!我已经启动并运行了!
每当我尝试引用 mshtml.IHTMLDocument2 的parentWindow 和/或将其分配给 mshtml.IHTMLWindow2 窗口对象时,引发该问题的原因都
与线程有关。
对于某些我不知道的原因,mshtml.IHTMLWindow 的 COM 对象似乎在另一个必须处于单线程单元 (STA) 状态的线程上运行。
所以诀窍是,在具有 STA 状态的另一个线程上调用/执行所需的代码段。
下面是一个示例代码:
我们现在还可以引用 mshtml.IHTMLDocument2 对象的parentWindow 并执行站点脚本和/或我们自己的脚本(记住它必须在 STA 线程上):
这可能会让人免于在未来。哈哈
FINALLY !! I got it up and running !
The reason for the
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:
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):
This might save someone from headaches in the future. lol