Imports mshtml
Imports System.Text
Imports System.Runtime.InteropServices

Public Class IEDom

    Declare Function ObjectFromLresult Lib "oleacc" (ByVal lResult As Int32, ByRef riid As System.Guid, ByVal wParam As Int32, ByRef ppvObject As IHTMLDocument) As Int32
    Dim win32 As New Win32Functions()

    Public Function IEDOMFromhWnd(ByVal hWnd As IntPtr) As IHTMLDocument
        Dim IID_IHTMLDocument As System.Guid = New System.Guid("626FC520-A41E-11CF-A731-00A0C9082637")
        'Dim hWndChild As Int32
        Dim lRes As Int32
        Dim lMsg As Int32
        Dim hr As Int32
        If Not hWnd.Equals(0) Then
            If Not hWnd.Equals(0) Then
                ' Register the message
                lMsg = win32.RegisterWindowMessage("WM_HTML_GETOBJECT")
                'MsgBox(lMsg.ToString)
                ' Get the object
                Call win32.SendMessageTimeout(hWnd, lMsg, 0, 0, _
                     win32.SMTO_ABORTIFHUNG, 1000, lRes)

                If lRes Then
                    ' Get the object from lRes
                    hr = ObjectFromLresult(lRes, IID_IHTMLDocument, 0, IEDOMFromhWnd)
                    If hr Then Throw New COMException(hr)
                End If
            End If
        Else
            MsgBox("bad hwnd!!")
        End If
    End Function

End Class
