[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

AppBar, WebBrowser Control and Internet Explorer URL gives InvalidCastException

Posted on 2007-10-17
11
Medium Priority
?
794 Views
Last Modified: 2013-12-17
I have created an AppBar in c# that docks to the LEFT of the desktop. The AppBar has a WebBrowser control in it. The AppBar then attempts to get the currently active Internet Explorer and capture the BeforeNavigate2 event so that I can later use the changed URL.

foreach (SHDocVw.InternetExplorer ie in shellWindows)
 {
                ie.BeforeNavigate2 += ie_BeforeNavigate2;
}

This does capture the event, however when I try to call a function in the webbrowser control after this event with the following code, I get an InvalidCastException:

public void ie_BeforeNavigate2(object pDisp, ref object url, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
        {
            System.Console.Out.WriteLine("Process URL: " + url);

            try
            {
                object ret = this.webBrowser1.Document.InvokeScript("callFunc");
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine(e.ToString());
            }            
        }

It appears that any reference to webBrowser1.document can't be made. I am unclear how to resolve this.

0
Comment
Question by:webbrian
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20097561
The Document property is public, and you should be able to reference it.
1. Did you set the Document in your WebBrowserControl?
2. Break the statement up a bit to see where it fails, as:
try
{
      HtmlDocument document = this.webBrowser1.Document;
      object ret = document.InvokeScript("callFunc");
}

Jim
0
 

Author Comment

by:webbrian
ID: 20097597
I changed the code as you suggested and it breaks at the HtmlDocument document = this.webBrowser1.Document; line

Here is the stack trace it gives:

A first chance exception of type 'System.InvalidCastException' occurred in System.Windows.Forms.dll
System.InvalidCastException: Specified cast is not valid.
   at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
   at System.Windows.Forms.WebBrowser.get_Document()
   at WindowsApplication1.MainForm.ie_BeforeNavigate2(Object pDisp, Object& url, Object& Flags, Object& TargetFrameName, Object& PostData, Object& Headers, Boolean& Cancel) in C:\Users\Brian\Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:line 39
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20097612
What did you set the Document to in webBrowser1?

Jim
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:webbrian
ID: 20097628
I didn't set the Document, I simply have a URL property set (it does fetch this url and displays it fine). So it should get that URL and automatically set the document to the page fetched in the URL property. Is this incorrect?
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20097657
It sounds like it loaded OK. I'll keep looking...

Jim
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20102135
I did find a reference to the BeforeNavigate signature:

private void BeforeNavigate(string url, int flags, string targetFrameName, ref object postData, string headers, ref bool cancel)

If you need something special from the BeforeNavigate2 signature, then I can keep looking.

Bob
0
 

Author Comment

by:webbrian
ID: 20105274
Well, I wonder if this is some sort of threading issue, because it seems the problem is that it can't access the WebBrowser.document, and perhaps the BeforeNavigate is in a different thread? Could this be the problem? If so, how can I get around this?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20117936
It isn't clear what the problem is, but it looks like a bug, since it is coming from inside the framework at this line:

System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()

That is a simple p/invoke:

[return: MarshalAs(UnmanagedType.Interface)]
UnsafeNativeMethods.IHTMLLocation GetLocation();
 
and, the interface is defined like this:

[Guid("163BB1E0-6E00-11CF-837A-48DC04C10000"), ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsDual), SuppressUnmanagedCodeSecurity]
internal interface IHTMLLocation
{
    void SetHref([In] string p);
    string GetHref();
    void SetProtocol([In] string p);
    string GetProtocol();
    void SetHost([In] string p);
    string GetHost();
    void SetHostname([In] string p);
    string GetHostname();
    void SetPort([In] string p);
    string GetPort();
    void SetPathname([In] string p);
    string GetPathname();
    void SetSearch([In] string p);
    string GetSearch();
    void SetHash([In] string p);
    string GetHash();
    void Reload([In] bool flag);
    void Replace([In] string bstr);
    void Assign([In] string bstr);
}

Here is the WebBrowser.Document get:

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HtmlDocument Document
{
    get
    {
        object obj2 = this.AxIWebBrowser2.Document;
        if (obj2 != null)
        {
            UnsafeNativeMethods.IHTMLDocument2 document = obj2 as UnsafeNativeMethods.IHTMLDocument2;
            if (document != null)
            {
                UnsafeNativeMethods.IHTMLLocation location = document.GetLocation();
                if (location != null)
                {
                    string href = location.GetHref();
                    if (!string.IsNullOrEmpty(href))
                    {
                        Uri url = new Uri(href);
                        EnsureUrlConnectPermission(url);
                        return new HtmlDocument(this.ShimManager, document as UnsafeNativeMethods.IHTMLDocument);
                    }
                }
            }
        }
        return null;
    }
}

Bob
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591118
Forced accept.

Computer101
EE Admin
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question