Accessing Frames throws InvalidCastException

Add-in Express™ Support Service
That's what is more important than anything else

Accessing Frames throws InvalidCastException
 
Steve Syfuhs




Posts: 8
Joined: 2014-07-29
The exception and stack trace is the one provided in my first post. It's consistently the same exception and at the same call stack.
Posted 07 Aug, 2014 11:08:38 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Steve,

This works for me on a page based on your HTML:

private void IEModule_DocumentComplete2(object pDisp, string url, bool rootDocLoaded) {
    System.Diagnostics.Debug.WriteLine("!!! IEModule_DocumentComplete2. url='" + url + "'");
    if (!rootDocLoaded || this.HTMLDocument == null) {
        System.Diagnostics.Debug.WriteLine("!!! IEModule_DocumentComplete2. Exit");
        return;
    }

    System.Diagnostics.Debug.WriteLine("!!! IEModule_DocumentComplete2. readyState=" + this.HTMLDocument.readyState.ToString());

    object frame = this.GetTargetWindow("name", 1, this.HTMLDocument);

    if (frame != null) {
        System.Diagnostics.Debug.WriteLine("!!! IEModule_DocumentComplete2. frame != null");
    } else {
        System.Diagnostics.Debug.WriteLine("!!! IEModule_DocumentComplete2. frame == null");
    }
}

private object GetTargetWindow(string getFrameBy, string frameVal, mshtml.HTMLDocument htmlDocument) {
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. Before htmlDocument.frames");
    var framesCollection = htmlDocument.frames; // this tells me its something internally in the frames getter 
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. After htmlDocument.frames");

    if (framesCollection.length == 0) return null;

    object parameter = frameVal;
    object targetWin = null;

    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. Before htmlDocument.frames.item(frameVal)");
    targetWin = htmlDocument.frames.item(ref parameter);
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. After htmlDocument.frames.item(frameVal)");

    return targetWin;
}

private object GetTargetWindow(string getFrameBy, int index, mshtml.HTMLDocument htmlDocument) {
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. Before htmlDocument.frames");
    var framesCollection = htmlDocument.frames; // this tells me its something internally in the frames getter 
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. After htmlDocument.frames");
                        
    if (framesCollection.length == 0) return null;
            
    object parameter = index;
    object targetWin = null;

    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. Before htmlDocument.frames.item(frameVal)");
    targetWin = htmlDocument.frames.item(ref parameter);
    System.Diagnostics.Debug.WriteLine("!!! GetTargetWindow. After htmlDocument.frames.item(frameVal)");

    return targetWin;
}


I've been told that the frames can load in a different order. If this is the case for you, try using try/catch. Also, frames are obsolete in HTML 5, see http://www.w3.org/TR/html5-diff/#obsolete-elements.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 08 Aug, 2014 06:04:49 Top
Steve Syfuhs




Posts: 8
Joined: 2014-07-29
Thanks Andrei,
Let me see if I can rewrite this a bit based on your sample code. I'm wondering if it may also be environmental as well.

Unfortunately I have to support what our customers use, and they use sites with frames. I would love to not support frames as per HTML 5. :)
Posted 08 Aug, 2014 10:47:27 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Steve,

Any news?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 12 Aug, 2014 06:37:34 Top
Steve Syfuhs




Posts: 8
Joined: 2014-07-29
Apologies for the delayed response. I've gone through the various options you've provided and they all end up throwing the same exception. I also ran this on another machine just to verify it wasn't a localized environmental issue.

My next step is to pull this code into its own project and run it on it's own to see if I reduce the deviations from the default project template and reproduce it.
Posted 12 Aug, 2014 13:24:34 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Steve,

Have you solved this?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Aug, 2014 01:32:18 Top