vb.net跨域访问疑虑。
Imports SystemImports 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)
以上是网上搜索到的,但是,使用方法中的Dim win As mshtml.IHTMLWindow2 = tmpframe.window,tmpframe.window怎么用啊?