1. On Saturday September 3RD, 2016 this community will be shutdown permanently.

    The time has come to close this community. I no longer have any interest in Visual Studio programming and my efforts to find someone to take over the community has failed.

    If anyone is interested in acquiring the database contact me and I will make it available for you.

get the current url from the active firefox window

Discussion in 'Visual Basic .Net' started by DN Admin, May 6, 2012.

  1. DN Admin

    DN Admin Administrator
    Staff Member

    United States
    Aug 7, 2010
    Likes Received:
    Joliet, IL U.S.A.
    I have this code, with wich I'm able to get the current url of a running ie instance.

    <pre class="prettyprint lang-vb Imports System.Text
    Imports System.Runtime.InteropServices.Marshal

    Module geturl
    Private Delegate Function EnumProcDelegate(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean 'Delegate added
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumProcDelegate, ByVal lParam As IntPtr) As Boolean
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As IntPtr) As IntPtr
    Private Const WM_GETTEXT = &HD
    Private Const WM_GETTEXTLENGTH = &HE
    Private Const GW_CHILD = 5
    Private Const GW_HWNDNEXT = 2
    Private Const GW_HWNDFIRST = 0
    Private sURL As String
    Private sClassName As String = "Edit" 'Default
    Private title As String
    Private sIndent As String 'Spaces

    Public Function GetCurrentUrl(ByVal hwnd As IntPtr) As String
    sClassName = ""
    sURL = ""
    sIndent = ""
    EnumWindows(AddressOf EnumProc, hwnd) 'hwnd - originally IntPtr.Zero
    Return sURL
    End Function

    Private Function EnumProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
    ' Enumerate the windows
    ' Find the URL in the browser window
    Dim buf As StringBuilder = New StringBuilder(256) 'String * 1024
    Dim length As Integer

    ' Get the window's title.
    length = GetWindowText(hWnd, buf, buf.Capacity)
    title = Left(buf.ToString, length)

    If title <> "" Then
    If title.Contains("Mozilla Firefox") Or title.Contains("Internet Explorer") Or title.Contains("Chrome") Then
    ' This is it. Find the URL information.
    sURL = EditInfo(hWnd)
    Return False
    End If
    End If
    ' Continue searching
    Return True

    End Function
    Private Function WindowText(ByVal window_hwnd As IntPtr) As String
    Dim txtlen As Integer
    Dim txt As String

    txt = "" 'WindowText = ""
    If window_hwnd = 0 Then Return "" 'Exit Function

    'Get the size of the window text
    txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
    If txtlen = 0 Then Return "" 'Exit Function

    'Extra for terminating char
    txtlen = txtlen + 1

    'Alloc memory for the buffer that recieves the text
    Dim buffer As IntPtr = AllocHGlobal(txtlen)

    'Send The WM_GETTEXT Message
    txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, buffer) 'byval txt

    'Copy the characters from the unmanaged memory to a managed string
    txt = PtrToStringAnsi(buffer)
    Return Left(txt, txtlen)
    End Function
    Private Function EditInfo(ByVal window_hwnd As IntPtr) As String
    Dim txt As String = ""
    Dim buf As String
    Dim buflen As Integer
    Dim child_hwnd As IntPtr
    Dim children() As IntPtr = {}
    Dim num_children As Integer
    Dim i As Integer

    'Get the class name.
    buflen = 256
    buf = Space(buflen - 1)
    buflen = GetClassName(window_hwnd, buf, buflen)
    buf = Left(buf, buflen)

    If title.Contains("Mozilla Firefox") Then
    sClassName = "MozillaContentWindowClass"
    ElseIf title.Contains("Internet Explorer") Then
    sClassName = "Edit"
    ElseIf title.Contains("Chrome") Then
    sClassName = "Chrome_AutocompleteEditView"
    End If

    ' See if we found an Edit/AutocompleteEditView object.
    If buf = sClassName Then
    Return WindowText(window_hwnd)
    End If

    ' It's not an Edit/AutocompleteEditView object. Search the children.
    ' Make a list of the child windows.
    num_children = 0
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    While child_hwnd <> 0
    num_children = num_children + 1
    ReDim Preserve children(0 To num_children) 'was 1 to ..
    children(num_children) = child_hwnd
    child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
    End While

    ' Get information on the child windows.
    sIndent &= " "
    For i = 1 To num_children
    txt = EditInfo(children(i))
    If txt <> "" Then Exit For
    Next i
    sIndent = Left(sIndent, sIndent.Length - 4)

    Return txt
    End Function
    End Module</pre>
    I simply call GetCurrentUrl(handle of the ie) and that's it.
    But I'm unable to get the current url of the firefox window. I have already changed the

    <pre class="prettyprint lang-vb sClassName = "MozillaContentWindowClass"</pre>
    to everything, but no succes.
    I have googled a many too, and it seems, that others have this problem too.
    Can you help me out?

    View the full article

Share This Page