private void button1_Click(object sender, RoutedEventArgs e)
        {
            ShellWindows shellwindows = new ShellWindows();
            string filename;
            foreach (InternetExplorer ie in shellwindows)
            {

                // Get the document 
                mshtml.IHTMLDocument document = ((mshtml.IHTMLDocument)ie.Document);
                // Get the script object. 
                object script = document.Script;

                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

                if (filename.Equals("iexplore") && ie.Document.parentWindow.name = "Test")
                {
                    // Make the call: 
                    string selectedColour = listBox1.SelectedValue.ToString();
                    ListBoxItem lbi = (listBox1.SelectedItem as ListBoxItem);

                    string[] colour = new string[] { lbi.Content.ToString() };
                    script.GetType().InvokeMember("setBackgroundColour", BindingFlags.InvokeMethod, null, script, colour);

                    // Release the script object, and possibly the document object. 
                    Marshal.ReleaseComObject(script);
                }
            }

Hi,

I am trying to call a javascript funtion on a webpage from a winform application. The above code works in .NET 4.0 but in 3.5 the line

object script = document.Script;

throws a "Specified cast is not valid." exception!

Any thoughts? I dont think anything I am using is .Net 4.0 specific?

Many Thanks

You need to cast it into some type, you can use statements like :

object script = ((object)document.Script);

although i do not understand why it gives you problems since everything in .NET is an object from System.object.

One thing to notice is that C# 4.0 uses dynamic type which some how is unknown until run time. so theoretically you may say 3.5 is abit stick than 4.0

Edited 6 Years Ago by mshauny: n/a

I have altered the code to that below, this seems to get me a step further, however it now fails when calling "InvokeMember" throwing a COMException: "Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"

Thanks

ShellWindows shellwindows = new ShellWindows();
            string filename;

            foreach (InternetExplorer ie in shellwindows)
            {
                // Get the script object. 
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

                if (filename.Equals("iexplore"))
                {
                    // Get the document 
                    IHTMLDocument2 document = (IHTMLDocument2)ie.Document;

                    string[] colour = new string[] { "yellow" };
                    foreach (object s in document.scripts)
                    {
                        try
                        {
                            s.GetType().InvokeMember("setBackgroundColour", BindingFlags.InvokeMethod, null, s, null);

                            // Release the script object, and possibly the document object. 
                            Marshal.ReleaseComObject(s);
                        }
                        catch { }
                    }
                }
            }
This article has been dead for over six months. Start a new discussion instead.