Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266063
  • 博文数量: 54
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 541
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-27 23:26
文章分类

全部博文(54)

文章存档

2018年(2)

2015年(3)

2014年(6)

2013年(5)

2012年(5)

2011年(7)

2010年(14)

2009年(1)

2008年(3)

2007年(6)

2006年(1)

2005年(1)

我的朋友

分类: 嵌入式

2012-06-11 16:32:27

'在项目中引用 mshtml和SHDocVw
'源码如下:
Imports System
Imports System.Runtime.InteropServices
Imports mshtml
Imports SHDocVw
Namespace CodecentrixSample
    Public Class CrossFrameIE
        Const E_ACCESSDENIED As Integer = &H80070005
        Private Shared IID_IWebBrowserApp As Guid = New Guid("0002DF05-0000-0000-C000-000000000046")
        Private Shared IID_IWebBrowser2 As Guid = New Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E")
        ' Returns null in case of failure.
        Public Shared Function GetDocumentFromWindow(ByVal htmlWindow As IHTMLWindow2) As IHTMLDocument2
            If htmlWindow Is Nothing Then
                Return Nothing
            End If
            ' First try the usual way to get the document.
            Try
                Dim doc As IHTMLDocument2 = htmlWindow.document
                Return doc
            Catch comEx As COMException
                ' I think COMException won't be ever fired but just to be sure ...
                If (comEx.ErrorCode <> E_ACCESSDENIED) Then
                    Return Nothing
                End If
            Catch ex As System.UnauthorizedAcces***ception

            Catch ex As Exception
                ' Any other error.
                Return Nothing
            End Try
            ' At this point the error was E_ACCESSDENIED because the frame contains a document from another domain.
            ' IE tries to prevent a cross frame scripting security issue.
            Try
                ' Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
                Dim sp As IServiceProvider = htmlWindow.window
                Dim sp1 As IServiceProvider = CType(htmlWindow, IServiceProvider)
                ' Use IServiceProvider.QueryService to get IWebBrowser2 object.
                Dim brws As Object = Nothing
                sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, brws)
                ' Get the document from IWebBrowser2.
                Dim browser As SHDocVw.WebBrowser = brws     '.IWebBrowser2 = brws
                Return browser.Document
            Catch
                Return Nothing
            End Try
        End Function
    End Class
    ' This is the COM IServiceProvider interface, not System.IServiceProvider .Net interface!
    _
    Public Interface IServiceProvider
        _
     Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As Object) As Integer
    End Interface
End Namespace
 
使用方法:
Dim win As mshtml.IHTMLWindow2 = tmpframe.window
Dim doc As mshtml.IHTMLDocument2 = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(win)
MessageBox.Show(doc.body.innerHTML)
阅读(1855) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~