Exemplo n.º 1
0
        /// <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);
        }