简体   繁体   中英

IHTMLDocument2 - click button inside iframe

I'm creating a DCOM extension for some really old application. Main application has IE control inside of it.

Using C# I was able to get handler for that IE Control as below:

Process pr = Process.GetCurrentProcess();

var title = pr.MainWindowTitle;
if(title.IndexOf("My old application -")<0)
{
    MessageBox.Show("No such window");
    return;
}
var wlwWindow = pr.MainWindowHandle;
var ieWindow = PI.FindWindowRecursive(wlwWindow, "Internet Explorer_Server", null);
if (ieWindow == IntPtr.Zero)
{
    MessageBox.Show("Unable to locate IE window.");
    return;
}
var myDocument= PI.GetIEDocumentFromWindowHandle(ieWindow);
MessageBox.Show(myDocument.url);

And here are my helper methods:

    public static IntPtr FindWindowRecursive(IntPtr parent, string windowClass, string windowCaption)
    {
        var found = FindWindowEx(parent, IntPtr.Zero, windowClass, windowCaption);
        if (found != IntPtr.Zero)
        {
            return found;
        }

        var child = FindWindowEx(parent, IntPtr.Zero, null, null);
        while (child != IntPtr.Zero)
        {
            found = FindWindowRecursive(child, windowClass, windowCaption);
            if (found != IntPtr.Zero)
            {
                return found;
            }
            child = GetNextWindow(child, 2);
        }
        return IntPtr.Zero;
    }

    public static IHTMLDocument2 GetIEDocumentFromWindowHandle(IntPtr hWnd)
    {
        IHTMLDocument2 htmlDocument = null;
        if (hWnd != IntPtr.Zero)
        {
            uint lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
            UIntPtr lResult;
            SendMessageTimeout(hWnd, lMsg, UIntPtr.Zero, UIntPtr.Zero,
                SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);
            if (lResult != UIntPtr.Zero)
            {
                htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;
                if (htmlDocument == null)
                {
                    throw new COMException("Unable to cast to an object of type IHTMLDocument");
                }
            }
        }
        return htmlDocument;
    }

This part works fine and I was able to get content of that html page from temp:

<body id="state" class="saveFavorite" onload="load()" onunload="RUIMaster.Destroy()" style="margin: 0px; padding: 0px; overflow: hidden;">
<p style="display: none;">
<object classid="clsid:22FD36F1-A133-11d4-A0E4-005056E2D8AA" height="0" width="0" id="RUIMaster">
</object>
</object>
<form>
<input id="id" type="hidden" value="" />
</form>
</p>
<iframe name="AAMain" style="margin: 0px; border: 0px; padding: 0px;" id="contents" src="rdaui_frame.htm" width="100%" height="100%" frameborder="0" />
</body>

I must access iframe with name AAMain .
Content of that iframe document looks like so:

<frameset cols="135,*" border="0" frameborder="no" framespacing="0">
    <frame name="Menu" scrolling="no" noresize marginwidth="0" marginheight="0" src="blank.htm">
    <frameset rows="310,*,31">
        <frame name="Top" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Centre" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Bottom" noresize marginwidth="0" marginheight="0" src="blank.htm">
    </frameset>
</frameset>

So basically I must access frame name Centre inside iframe named AAMain

myDocument -> iframe(AAMain) -> frame(Centre) -> and then click on image

I was trying to iterate over frames of myDocument but I get invalid cast exception.

try
{
    FramesCollection frames = myDocument.frames;
    object index = 0;
    IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref index);
    var xx = (HTMLDocument)frame.document;
    MessageBox.Show(xx.body.innerHTML);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

even code like this:

MessageBox.Show(myDocument.frames.length.ToString());

gives me the same cast exception.

This should be possible via contentWindow or contentDocument property of iframe element depending on what IE version you have according to http://www.w3schools.com/jsref/prop_frame_contentwindow.asp However I haven't been able to get it working, contentWindow seems to return some element but it has empty document property (in my case with IE9).

Since this question has been posted a while ago, I'm wondering if you managed to make some progress with this?

Try accessing the frames collection using single threaded architecture (STA Thread). That worked for me, in a functional nunit test that confirmed frame contents. I had the same cast error until I switched thread models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM