none
VC++ - WebBrowser and InvokeScript with parameters RRS feed

  • Question

  • Hi everybody,

    I'm working with VS2008 and I'm coding in C++.Net
    In my application, I have a WebBrowser Control which display a HTML file containing a Javascript function with this header :

    function update( new_centerLat, new_centerLon, new_zoom )
    


    I would like to call this function. I tried this code :

    String^ strJavascriptFunction = "update";
    
    cli::array<Object^>^ myObjArray = {49.4561, 3.12374, 9};
    
    wbGoogleMap->Document->InvokeScript ( strJavascriptFunction, myObjArray  );
    

    This code compile but when it's executed, an exception is thrown which say the specified cast isn't valid...

    I don't understand why... Any idea?
    Thursday, August 6, 2009 1:19 PM

Answers

  • Internet Explorer (the machine behind WebBrowser) is a single-threaded component.  You should only use it from your main (UI) thread.  You can use Control::Invoke() to marshal the call from the background thread to the UI thread.

    Hans Passant.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 3:20 AM
    Friday, August 7, 2009 12:41 PM
    Moderator

All replies

  • Not sure, if you have Visual Studio 2008 and set up Micorosoft's symbol server, you can step through .Net's source code.

    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Thursday, August 6, 2009 6:50 PM
    Moderator
  • Thx for your reply !

    I did some new tests and something strange happened... I tried that :


    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
     {
    wbGoogleMap->Document->InvokeScript ( "update", gcnew cli::array<Object^>{49, 3, 9} );
    }

    And it works! But I need to execute the script from a thread. But when I tried this code :


    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    System::Threading::ThreadStart^ tZoom = gcnew System::Threading::ThreadStart( this, &MainForm::ThreadZoom);
    					 threadZoom = gcnew System::Threading::Thread (tZoom);
    threadZoom->Start();
    }
    
    private: System::Void ThreadZoom ( )
    {
    wbGoogleMap->Document->InvokeScript ( "update", gcnew cli::array<Object^>{49, 3, 9} );
    }

    It throws exception "the specified cast is not valid" on the "invokeScript" instruction...

    I don't understand why, it's very strange...
    Friday, August 7, 2009 7:40 AM
  • Internet Explorer (the machine behind WebBrowser) is a single-threaded component.  You should only use it from your main (UI) thread.  You can use Control::Invoke() to marshal the call from the background thread to the UI thread.

    Hans Passant.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 3:20 AM
    Friday, August 7, 2009 12:41 PM
    Moderator