Пример #1
0
        // document를 일반적으로 가져올 수 없는 경우 (security issue) 이렇게 가져옴.
        public static IHTMLDocument2[] GetHtmlDocumentsByOle(IHTMLDocument2 doc)
        {
            try
            {
                ExceptionTester.Instance.Throw("gethtmlbyole");

                List <IHTMLDocument2> docs = new List <IHTMLDocument2>();

                // add main doc
                docs.Add(doc);

                //get the OLE container from the window's parent
                IOleContainer oc = doc as IOleContainer;                      //OC ALLOC

                //get the OLE enumerator for the embedded objects
                int          hr = 0;
                IEnumUnknown eu;
                hr = oc.EnumObjects(tagOLECONTF.OLECONTF_EMBEDDINGS, out eu);                 //EU ALLOC

                Guid      IID_IWebBrowser2 = typeof(SHDocVw.IWebBrowser2).GUID;
                object    pUnk             = null;
                int       fetched          = 0;
                const int MAX_FETCH_COUNT  = 1;

                for (int i = 0; eu.Next(MAX_FETCH_COUNT, out pUnk, out fetched) == hr; i++)
                {
                    //QI pUnk for the IWebBrowser2 interface
                    SHDocVw.IWebBrowser2 brow = pUnk as SHDocVw.IWebBrowser2;

                    if (brow != null)
                    {
                        IHTMLDocument2 d = brow.Document as IHTMLDocument2;
                        //	Logger.Log("wb found. doc's url: {0}", d.url);

                        // if we found document, repeat recursively
                        IHTMLDocument2[] docs2 = GetHtmlDocumentsByOle(d);

                        foreach (IHTMLDocument2 doc2 in docs2)
                        {
                            docs.Add(doc2);
                        }
                    }
                }

                Marshal.ReleaseComObject(eu);                                                 //EU FREE

                return(docs.ToArray());
            }
            catch (Exception e)
            {
                Logger.Warn("cannt get html doc by ole! {0}", e.Message);

                return(new IHTMLDocument2[] { });
            }
        }
        public static LightWeightHTMLDocument[] GetLightWeightDocumentForFrames(IHTMLDocument2 htmlDocument)
        {
            ArrayList frameLightWeightDocuments = new ArrayList();

            // Get the IOleContainer for the for the html document (this requires that
            // the document is the root document in the browser)
            IOleContainer oleContainer = (IOleContainer)htmlDocument;
            IEnumUnknown  enumUnknown;

            // Enumerate the controls in the browser
            oleContainer.EnumObjects(OLECONTF.EMBEDDINGS, out enumUnknown);

            // Iterate through the controls
            object unknown;

            for (int i = 0; HRESULT.S_OK == enumUnknown.Next(1, out unknown, IntPtr.Zero); i++)
            {
                // Only subframes should cast to IWebBrowser2
                IWebBrowser2 webBrowser = unknown as IWebBrowser2;

                // Since it is a subframe, we can also get the base frame implementation for it
                IHTMLFrameBase frameBase = unknown as IHTMLFrameBase;

                // It's a frame, add this to the list!
                if (webBrowser != null)
                {
                    try
                    {
                        IHTMLDocument2 frameDocument = webBrowser.Document as IHTMLDocument2;

                        if (frameDocument != null)
                        {
                            LightWeightHTMLDocument document = LightWeightHTMLDocument.FromIHTMLDocument2(frameDocument, frameDocument.url, frameBase.name);
                            if (document != null)
                            {
                                frameLightWeightDocuments.Add(document);
                            }
                        }
                    }
                    catch (InvalidCastException)
                    {
                        string html = "<HTML></HTML>";
                        LightWeightHTMLDocument document = LightWeightHTMLDocument.FromString(html, webBrowser.LocationURL, webBrowser.LocationURL, true);
                        if (document != null)
                        {
                            frameLightWeightDocuments.Add(document);
                        }
                    }
                }
            }
            return((LightWeightHTMLDocument[])frameLightWeightDocuments.ToArray(typeof(LightWeightHTMLDocument)));
        }
Пример #3
0
        internal static void EnumIWebBrowser2Interfaces(IWebBrowser2Processor processor)
        {
            IEnumUnknown  eu;
            IOleContainer oc = processor.HTMLDocument() as IOleContainer;
            int           hr = oc.EnumObjects(tagOLECONTF.OLECONTF_EMBEDDINGS, out eu);

            Marshal.ThrowExceptionForHR(hr);

            try
            {
                object    pUnk;
                int       fetched;
                const int MAX_FETCH_COUNT = 1;

                // get the first embedded object
                // pUnk alloc
                hr = eu.Next(MAX_FETCH_COUNT, out pUnk, out fetched);
                Marshal.ThrowExceptionForHR(hr);

                while (hr == 0)
                {
                    // Query Interface pUnk for the IWebBrowser2 interface
                    IWebBrowser2 brow = pUnk as IWebBrowser2;

                    try
                    {
                        if (brow != null)
                        {
                            processor.Process(brow);
                            if (!processor.Continue())
                            {
                                break;
                            }
                            // free brow
                            Marshal.ReleaseComObject(brow);
                        }
                    }
                    catch
                    {
                        if (brow != null)
                        {
                            // free brow
                            Marshal.ReleaseComObject(brow);
                        }
                        // pUnk free
                        Marshal.ReleaseComObject(pUnk);
                    }

                    // pUnk free
                    Marshal.ReleaseComObject(pUnk);

                    // get the next embedded object
                    // pUnk alloc
                    hr = eu.Next(MAX_FETCH_COUNT, out pUnk, out fetched);
                    Marshal.ThrowExceptionForHR(hr);
                }
            }
            finally
            {
                // eu free
                Marshal.ReleaseComObject(eu);
            }
        }