58034

IHTMLDocument2 - click button inside iframe

Question:

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>

<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.<br /> 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 <strong>frame</strong> name <strong>Centre</strong> inside <strong>iframe</strong> named <strong>AAMain</strong>

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.

Answer1:

This should be possible via contentWindow or contentDocument property of iframe element depending on what IE version you have according to <a href="http://www.w3schools.com/jsref/prop_frame_contentwindow.asp" rel="nofollow">http://www.w3schools.com/jsref/prop_frame_contentwindow.asp</a> 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?

Answer2:

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.

Recommend

  • QC Connection from Java using Com4
  • Active directory : get groups where a user is member
  • Excel Interop Workbooks.Open inconsistencies
  • Is dynamic compilation in a 'ASP.NET Web Application' possible?
  • WIA services 2, download and install on windows xp / 7
  • Is there a greater chance to collide when comparing GUIDs based on a hash vs \"Guid.NewGuid()?
  • RavenDB indexing errors
  • how to set to NULL all the filestream varbinary(max) fields?
  • Multiple Left Join LINQ-to-entities
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • encoding issues with content in response from HttpWebRequest
  • Suqueries in select clause with JPA
  • Cloud Code function running twice
  • Default parameter as generic type
  • Thread safety of a fluent like class using clone() and non final fields
  • Converting a WriteableBitmap image ToArray in UWP
  • Lost migrations and Azure database is now out of sync
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • JavaScriptCore crash on iOS9
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Importing jscolor library in angular 2
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • WOWZA + RTMP + HTML5 Playback?
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Binding checkboxes to object values in AngularJs