IHTMLDocument2 - click button inside iframe

泄露秘密 提交于 2019-12-13 04:27:07

问题


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.


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/14439081/ihtmldocument2-click-button-inside-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!