Last active
January 8, 2021 02:27
-
-
Save kumatti1/485df5eaeaa43d374770 to your computer and use it in GitHub Desktop.
WinHttpとIPersistStreamInit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Private Type GUID | |
Data1 As Long | |
Data2 As Integer | |
Data3 As Integer | |
Data4(0 To 7) As Byte | |
End Type | |
Private Declare PtrSafe _ | |
Function IIDFromString Lib "ole32.dll" ( _ | |
ByVal lpsz As LongPtr, _ | |
lpiid As GUID _ | |
) As Long | |
Private Declare PtrSafe _ | |
Function DispCallFunc Lib "OleAut32.dll" ( _ | |
ByVal pvInstance As Any, _ | |
ByVal oVft As LongPtr, _ | |
ByVal cc_ As Long, _ | |
ByVal vtReturn As Integer, _ | |
ByVal cActuals As Long, _ | |
ByRef prgvt As Integer, _ | |
ByRef prgpvarg As LongPtr, _ | |
ByRef pvargResult As Variant _ | |
) As Long | |
Const CC_STDCALL = 4& | |
Sub hoge() | |
Dim pStreamInt As IUnknown 'IPersistStreamInit | |
Dim iid As GUID | |
Dim hr As Long | |
hr = IIDFromString(StrPtr("{7FD52380-4E07-101B-AE2D-08002B2EC713}"), iid) | |
Dim pDoc As MSHTML.IHTMLDocument2 | |
Set pDoc = New HTMLDocument | |
hr = CallComMethod(pDoc, 0, VarPtr(iid), VarPtr(pStreamInt)) | |
If pStreamInt Is Nothing Then Exit Sub | |
Dim http As WinHttp.WinHttpRequest | |
Set http = New WinHttp.WinHttpRequest | |
http.Open "GET", "http://www.yahoo.co.jp/", False | |
http.send | |
Dim tmp As IUnknown | |
Set tmp = http.ResponseStream | |
hr = CallComMethod(pStreamInt, 5, ObjPtr(tmp)) | |
While pDoc.readyState <> "complete" | |
DoEvents | |
Wend | |
Debug.Print pDoc.Title | |
End Sub | |
' COMのメソッド呼び出し | |
Private Function CallComMethod(ByVal obj As Variant, _ | |
ByVal VTBLIndex As Long, ParamArray Args() As Variant) As Long | |
Dim pArgs() As Long | |
Dim vt() As Integer | |
Dim vntResult As Variant | |
Dim lngCount As Long | |
Dim pObj As Long | |
Dim hr As Long | |
Dim i As Long | |
If IsObject(obj) Then | |
pObj = ObjPtr(obj) | |
ElseIf VarType(obj) = vbDataObject Then | |
pObj = ObjPtr(obj) | |
Else | |
pObj = obj | |
End If | |
If pObj = 0 Then Err.Raise 91 | |
lngCount = UBound(Args) + 1 | |
ReDim pArgs(0 To lngCount + (lngCount > 0)) | |
ReDim vt(0 To UBound(pArgs)) | |
For i = 0 To lngCount - 1 | |
vt(i) = VarType(Args(i)) | |
pArgs(i) = VarPtr(Args(i)) | |
Next | |
hr = DispCallFunc(pObj, VTBLIndex * 4, _ | |
CC_STDCALL, vbLong, _ | |
lngCount, vt(0), pArgs(0), vntResult) | |
If hr < 0 Then Err.Raise hr | |
If vntResult < 0 Then | |
If VTBLIndex <> 1 And VTBLIndex <> 2 Then | |
Err.Raise vntResult | |
End If | |
End If | |
CallComMethod = vntResult | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment