• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Retrieve web address from IE using VB.Net 2005

Hi All

I've written a small program using vb6 which involves retrieving the web address from the current active window of IE. This uses DDE to communicate with IE. ( .LinkTopic = "IExplore|WWW_GetWindowInfo")

I'm trying to convert this application to .net in order to expand on it. The problem is that DDE is not supported in .net 2005

I need to be able to retrieve the web address from the current active instance of IE using OLE Automation(which has apparently replaced DDE) and insert it into a textbox.

Any help will be greatly appreciated...thanks
0
Jayzed
Asked:
Jayzed
  • 2
2 Solutions
 
Bob LearnedCommented:
0
 
Bob LearnedCommented:
Here is the class for VB.NET:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ApiWindow
      Public MainWindowTitle As String
      Public ClassName As String
      Public hWnd As Int32
End Class

Public Class WindowsEnumerator

      Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer

      ' Top-level windows.
      Private Declare Function EnumWindows Lib "user32" _
       (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

      ' Child windows.
      Private Declare Function EnumChildWindows Lib "user32" _
       (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

      ' Get the window class.
      Private Declare Function GetClassName _
       Lib "user32" Alias "GetClassNameA" _
       (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer

      ' Test if the window is visible--only get visible ones.
      Private Declare Function IsWindowVisible Lib "user32" _
       (ByVal hwnd As Integer) As Integer

      ' Test if the window's parent--only get the one's without parents.
      Private Declare Function GetParent Lib "user32" _
       (ByVal hwnd As Integer) As Integer

      ' Get window text length signature.
      Private Declare Function SendMessage _
       Lib "user32" Alias "SendMessageA" _
       (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

      ' Get window text signature.
      Private Declare Function SendMessage _
       Lib "user32" Alias "SendMessageA" _
       (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32

      Private _listChildren As New ArrayList
      Private _listTopLevel As New ArrayList

      Private _topLevelClass As String = String.Empty
      Private _childClass As String = String.Empty

      Public Overloads Function GetTopLevelWindows() As ApiWindow()

            EnumWindows(AddressOf EnumWindowProc, &H0)

            Return _listTopLevel.ToArray(GetType(ApiWindow))

  End Function

  Public Overloads Function GetTopLevelWindows(ByVal className As String) As ApiWindow()

    _topLevelClass = className

    Return Me.GetTopLevelWindows()

  End Function

  Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As ApiWindow()

    ' Clear the window list.
    _listChildren = New ArrayList

    ' Start the enumeration process.
    EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)

    ' Return the children list when the process is completed.
    Return _listChildren.ToArray(GetType(ApiWindow))

  End Function

  Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As ApiWindow()

    ' Set the search
    _childClass = childClass

    Return Me.GetChildWindows(hwnd)

  End Function

  Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

    ' Eliminate windows that are not top-level.
    If GetParent(hwnd) = 0 AndAlso IsWindowVisible(hwnd) Then

      ' Get the window title / class name.
      Dim window As ApiWindow = GetWindowIdentification(hwnd)

      ' Match the class name if searching for a specific window class.
      If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
        _listTopLevel.Add(window)
      End If

    End If

    ' To continue enumeration, return True (1), and to stop enumeration
    ' return False (0).
    ' When 1 is returned, enumeration continues until there are no
    ' more windows left.

    Return 1

  End Function

  Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

    Dim window As ApiWindow = GetWindowIdentification(hwnd)

    ' Attempt to match the child class, if one was specified, otherwise
    ' enumerate all the child windows.
    If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
      _listChildren.Add(window)
    End If

    Return 1

  End Function

  Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow

    Const W_GETTEXT As Int32 = &HD
    Const W_GETTEXTLENGTH = &HE

    Dim window As New ApiWindow

    Dim title As New StringBuilder

    ' Get the size of the string required to hold the window title.
    Dim size As Int32 = SendMessage(hwnd, W_GETTEXTLENGTH, 0, 0)

    ' If the return is 0, there is no title.
    If size > 0 Then
      title = New StringBuilder(size + 1)

      SendMessage(hwnd, W_GETTEXT, title.Capacity, title)
    End If

    ' Get the class name for the window.
    Dim classBuilder As New StringBuilder(64)
    GetClassName(hwnd, classBuilder, 64)

    ' Set the properties for the ApiWindow object.
    window.ClassName = classBuilder.ToString()
    window.MainWindowTitle = title.ToString()
    window.hWnd = hwnd

    Return window

  End Function

End Class

Bob
0
 
JayzedAuthor Commented:
Sorry about that...I've been away on business for a couple of weeks and almost forgot about the question.
I've decided to continue developing this in VB6 due to reasons other than this.

Thanks anyway for your help

regards
John
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now