|
■No48318 (魔界の仮面弁士) に追記
> System.Runtime.InteropServices.ComTypes.IPersistFile インターフェイス、
> もしくは IPersistStreamInit インターフェイスを使ってみてください。
今度は IPersistStreamInit 版です。下記では Interface 定義から書いていますが、
Microsoft.BizTalk.Component.Interop.IPersistStreamInit や
Microsoft.VisualStudio.OLE.Interop.IPersistStreamInit のアセンブリがある場合は、
それらを利用できるかも知れません。
HttpWebResponse.GetResponseStream() を IPersistStreamInit に変換する際に、
1. ADODB.Stream に転写
2. ADODB.Stream を System.Runtime.InteropServices.ComTypes.IStream にキャスト
3. IPersistStreamInit.Load(IStream) で読み込み
という流れで処理していますが……もっとスマートな方法があるのかも。
Option Strict On
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Public Class Form1
<ComImport(), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _
Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")> _
Public Interface IPersistStreamInit
Sub GetClassID(<Out()> ByRef pClassID As Guid)
<PreserveSig()> Function IsDirty() As Integer
Sub Load(<[In](), MarshalAs(UnmanagedType.Interface)> _
ByVal pstm As IStream)
Sub Save(<[In](), MarshalAs(UnmanagedType.Interface)> _
ByVal pstm As IStream, _
<[In](), MarshalAs(UnmanagedType.Bool)> _
ByVal fClearDirty As Boolean)
Sub GetSizeMax(<Out(), MarshalAs(UnmanagedType.LPArray)> _
ByVal pcbSize As Long)
Sub InitNew()
End Interface
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim req As WebRequest = WebRequest.Create("http://www.google.co.jp/")
Dim istm As IStream
Using res As WebResponse = req.GetResponse(), _
stm As System.IO.Stream = res.GetResponseStream()
Dim aStm As New ADODB.Stream()
aStm.Type = ADODB.StreamTypeEnum.adTypeBinary
aStm.Open()
Dim count As Integer = 0
Do
Dim buf(1023) As Byte
count = stm.Read(buf, 0, buf.Length)
If count < buf.Length Then
ReDim Preserve buf(count - 1)
End If
aStm.Write(buf)
Loop Until count = 0
aStm.Flush()
aStm.Position = 0
istm = DirectCast(aStm, IStream)
End Using
Dim doc As New mshtml.HTMLDocument()
Dim ps As IPersistStreamInit = DirectCast(doc, IPersistStreamInit)
ps.InitNew()
ps.Load(istm)
'( uninitialized => ) loading => interactive => complete
Do Until doc.readyState = "complete"
Application.DoEvents()
Loop
MsgBox(doc.documentElement.outerHTML)
End Sub
End Class
|