get/set javascript variable from C#

C

Christopher Bray

Hello,

I am trying to retrieve a javascript variable that I have assigned to the "document" object of a hosted IE control.

I imported the IE control per the instructions at the following URL:

http://www.codeproject.com/csharp/webbrowser.asp

I have followed some examples that I found online at:

http://groups.google.com/groups?hl=...%2478b8b630%248df82ecf%40TK2MSFTNGXA02&rnum=2

I have an external class (not shown here) that calls the Navigate() method and directs the browser to an empty html file.

I receive the typical first-chance exceptions during the call to the EndInit() method, but I can't seem to get past the call to InvokeMember(). I receive a System.Runtime.InteropServices.COMException exception with an "Unknown error" message body.

The following code is what I am working with:


using System;
using System.Reflection;
using System.Windows.Forms;

using MSHTML;
using AxSHDocVw;

namespace eCore.Studio.Windows
{
/// <summary>
/// Summary description for BrowserControl.
/// </summary>
public class BrowserControl : UserControl
{
private AxWebBrowser pBrowser;

internal BrowserControl()
{
this.pBrowser = new AxWebBrowser();
this.pBrowser.BeginInit();
this.pBrowser.Dock = DockStyle.Fill;
this.Controls.Add(this.pBrowser);
this.pBrowser.EndInit();
this.pBrowser.RegisterAsBrowser = true;
this.pBrowser.RegisterAsDropTarget = true;
this.pBrowser.Silent = false;
this.pBrowser.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(this.HandleNavigateComplete);
}

public void Navigate(string url)
{
object o = null;
this.pBrowser.Navigate(url, ref o, ref o, ref o, ref o);
}

public IHTMLDocument Document
{
get
{
return (IHTMLDocument) this.pBrowser.Document;
}
}

public IHTMLDocument2 Document2
{
get
{
return (IHTMLDocument2) this.pBrowser.Document;
}
}

public IHTMLDocument3 Document3
{
get
{
return (IHTMLDocument3) this.pBrowser.Document;
}
}

public IHTMLWindow2 Window
{
get
{
return (IHTMLWindow2) this.Document2.parentWindow;
}
}

public void Execute(string script)
{
this.Window.execScript(script, "JavaScript");
}

private void HandleNavigateComplete(object sender, DWebBrowserEvents2_NavigateComplete2Event args)
{
this.Execute("document.API = 2;");
IHTMLWindow2 w = this.Window;
Type t = w.GetType();
object o = t.InvokeMember("document.API", BindingFlags.GetProperty, null, w, new object[] {});
}
}
}

Any assistance is appreciated!

Thanks,

Chris Bray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top