Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:40:54+00:00 2026-05-29T22:40:54+00:00

Using the .NET WebBrowser control, it is fairly simple to execute a member of

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:40:55+00:00Added an answer on May 29, 2026 at 10:40 pm

    FINALLY !! 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using the WebBrowser control in asp.net page. here is the simple code:
When using .NET's WebBrowser control, how can I get the entire HTML of the
When using the .NET WebBrowser control how do you open a link in a
I have been using the .NET WebBrowser control in edit mode as part of
I'm using th WebBrowser control in the .net 2.0 framework. Is it possible to
I'm using a WebBrowser control in my Visual C# (.net 2.0) application. Now I'd
If I'm using a .NET WebBrowser control, and I dynamically populate the HTML, JS
I'm using the WebBrowser control in an ASP.NET MVC 2 app (don't judge, I'm
Using the Microsoft .NET WebBrowser control sometimes leads to a White Screen. Yahoo.com is
I'm using a WebBrowser control in .NET CF 3.5 for a Windows CE device

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.