/// <summary> /// Helper Method to get the IHTMLDocument2 /// </summary> /// <param name="mainWindow"></param> /// <returns></returns> private static IHTMLDocument2 GetHtmlDocument(IInteropWindow mainWindow) { var ieServer = "Internet Explorer_Server".Equals(mainWindow.GetClassname()) ? mainWindow : mainWindow.GetChildren().FirstOrDefault(window => window.GetClassname() == "Internet Explorer_Server"); if (ieServer == null) { Log.Warn().WriteLine("No Internet Explorer_Server for {0}", mainWindow.Text); return(null); } var windowMessage = WindowsMessage.RegisterWindowsMessage("WM_HTML_GETOBJECT"); if (windowMessage == 0) { Log.Warn().WriteLine("Couldn't register WM_HTML_GETOBJECT"); return(null); } Log.Debug().WriteLine("Trying WM_HTML_GETOBJECT on {0}", ieServer.Classname); if (User32Api.TrySendMessage(ieServer.Handle, windowMessage, IntPtr.Zero, out UIntPtr response)) { var document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero); if (document2 != null) { return(document2); } Log.Error().WriteLine(null, "No IHTMLDocument2 found"); return(null); } Log.Error().WriteLine(null, "No answer on WM_HTML_GETOBJECT."); return(null); }
/// <summary> /// Helper Method to get the IHTMLDocument2 /// </summary> /// <param name="mainWindow"></param> /// <returns></returns> private static IHTMLDocument2 getHTMLDocument(WindowDetails mainWindow) { WindowDetails ieServer; if ("Internet Explorer_Server".Equals(mainWindow.ClassName)) { ieServer = mainWindow; } else { ieServer = mainWindow.GetChild("Internet Explorer_Server"); } if (ieServer == null) { LOG.WarnFormat("No Internet Explorer_Server for {0}", mainWindow.Text); return(null); } IHTMLDocument2 document2 = null; uint windowMessage = User32.RegisterWindowMessage("WM_HTML_GETOBJECT"); if (windowMessage == 0) { LOG.WarnFormat("Couldn't register WM_HTML_GETOBJECT"); return(null); } LOG.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName); UIntPtr response; User32.SendMessageTimeout(ieServer.Handle, windowMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 5000, out response); if (response != UIntPtr.Zero) { document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero); if (document2 == null) { LOG.Error("No IHTMLDocument2 found"); return(null); } } else { LOG.Error("No answer on WM_HTML_GETOBJECT."); return(null); } return(document2); }
/// <summary> /// Helper method which will retrieve the IHTMLDocument2 for the supplied window, /// or return the first if none is supplied. /// </summary> /// <param name="browserWindowDetails">The WindowDetails to get the IHTMLDocument2 for</param> /// <param name="document2">Ref to the IHTMLDocument2 to return</param> /// <returns>The WindowDetails to which the IHTMLDocument2 belongs</returns> private static DocumentContainer GetDocument(WindowDetails activeWindow) { DocumentContainer returnDocumentContainer = null; WindowDetails returnWindow = null; IHTMLDocument2 returnDocument2 = null; // alternative if no match WindowDetails alternativeReturnWindow = null; IHTMLDocument2 alternativeReturnDocument2 = null; // Find the IE window foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame")) { LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text); Accessible ieAccessible = null; WindowDetails directUIWD = GetDirectUI(ieWindow); if (directUIWD != null) { ieAccessible = new Accessible(directUIWD.Handle); } if (ieAccessible == null) { LOG.InfoFormat("Active Window is {0}", activeWindow.Text); if (!ieWindow.Equals(activeWindow)) { LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text); continue; } LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text); } try { // Get the Document IHTMLDocument2 document2 = null; uint windowMessage = User32.RegisterWindowMessage("WM_HTML_GETOBJECT"); if (windowMessage == 0) { LOG.WarnFormat("Couldn't register WM_HTML_GETOBJECT"); continue; } WindowDetails ieServer = ieWindow.GetChild("Internet Explorer_Server"); if (ieServer == null) { LOG.WarnFormat("No Internet Explorer_Server for {0}", ieWindow.Text); continue; } LOG.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName); UIntPtr response; User32.SendMessageTimeout(ieServer.Handle, windowMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out response); if (response != UIntPtr.Zero) { document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero); if (document2 == null) { LOG.Error("No IHTMLDocument2 found"); continue; } } else { LOG.Error("No answer on WM_HTML_GETOBJECT."); continue; } // Get the content window handle for the shellWindow.Document IOleWindow oleWindow = (IOleWindow)document2; IntPtr contentWindowHandle = IntPtr.Zero; if (oleWindow != null) { oleWindow.GetWindow(out contentWindowHandle); } if (contentWindowHandle != IntPtr.Zero) { // Get the HTMLDocument to check the hasFocus // See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/60c6c95d-377c-4bf4-860d-390840fce31c/ IHTMLDocument4 document4 = (IHTMLDocument4)document2; if (document4.hasFocus()) { LOG.DebugFormat("Matched focused document: {0}", document2.title); // Look no further, we got what we wanted! returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } try { if (ieWindow.Equals(activeWindow)) { returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } else if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption)) { LOG.DebugFormat("Title: {0}", document2.title); returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); } else { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } catch (Exception) { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } } catch (Exception e) { LOG.Error(e); LOG.DebugFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text); } } // check if we have something to return if (returnWindow != null) { // As it doesn't have focus, make sure it's active returnWindow.Restore(); returnWindow.GetParent(); // Create the container returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow); } if (returnDocumentContainer == null && alternativeReturnDocument2 != null) { // As it doesn't have focus, make sure it's active alternativeReturnWindow.Restore(); alternativeReturnWindow.GetParent(); // Create the container returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow); } return(returnDocumentContainer); }