How can I get the url of a web page from an external vb.net application

I have a VB.net application that is running on my desktop and I need to be able to identify the URL of the page that is loaded in a web browser such as Internet Explorer. I am able to identify the title that is shown in the title bar by using  the following API:

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal handle As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer

is there an API that can give me the address (URL)?
 
Viking46Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If you have an Internet Explorer instance up and running, you can use Internet Explorer automation to get that detail, but that doesn't work for other browser types.  You might be able to enumerate top-level windows, and get child windows, until you find the right class.
0
Viking46Author Commented:
If we concentrate on IE, can you give an example of how this can be done please?
0
Bob LearnedCommented:
Using Internet Explorer COM automation:


' Add a COM reference to 'Microsoft Internet Controls' to the project.
' Add a .NET reference to Microsoft.mshtml to the project.
 
' NOTE:
' Don't add an Imports mshtml if you want your Intellisense to work.
 
Imports System
Imports System.Collections.Generic
Imports SHDocVw
 
Public Class InternetExplorerWindows
 
  Public Shared Function GetWindows() As Dictionary(Of String, String)
    Dim list As New Dictionary(Of String, String)
    For Each window As InternetExplorer In New ShellWindows()
 
      ' Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2.
      If TypeOf window.Document Is mshtml.HTMLDocument Then
        list.Add(window.LocationURL, CType(window.Document, mshtml.HTMLDocument).body.innerHTML)
      End If
    Next window
    Return list
  End Function
 
  Public Shared Function GetIFrameHTML(ByVal frameID As String) As Dictionary(Of String, String)
    Dim list As New Dictionary(Of String, String)
    For Each window As InternetExplorer In New ShellWindows()
 
      ' Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2.
      If TypeOf window.Document Is mshtml.HTMLDocument Then
        Dim doc As mshtml.HTMLDocument = CType(window.Document, mshtml.HTMLDocument)
        If doc.getElementById(frameID) IsNot Nothing Then
          Dim frame As mshtml.HTMLIFrame = CType(doc.getElementById(frameID), mshtml.HTMLIFrame)
          Dim window2 As mshtml.HTMLWindow2 = CType(frame.contentWindow, mshtml.HTMLWindow2)
          Dim windowDocument As mshtml.IHTMLDocument2 = CType(window2.document, mshtml.IHTMLDocument2)
          list.Add(window.LocationName, windowDocument.body.outerHTML)
        End If
      End If
    Next window
    Return list
  End Function
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Viking46Author Commented:
This solution works well and does as I asked, thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development Software

From novice to tech pro — start learning today.