Solved

Obtaining current URL from browser

Posted on 1998-09-03
4
174 Views
Last Modified: 2010-04-30
I'd like to be able to obtain the URL for a page being displayed in a browser. If you can post an api call (from shell32.dll?) to fetch the URL please show me. -Roy
0
Comment
Question by:royster
  • 2
4 Comments
 

Expert Comment

by:tnatsni
Comment Utility
You could use DDE...  There's DDE info for Netscape.
The following is the Command you would use with Netscape.
----
WWW_GetWindowInfo

     Netscape is: Server.
     Transaction Type: XTYP_REQUEST.
     Item (Arguments): dwWindowID
          dwWindowID is the Netscape window to obtain current information on. This value can be 0xFFFFFFFF to specify the last active Netscape window.
     Data (Returns): qcsURL,qcsTitle
          qcsURL is the current URL loaded in the window.
          qcsTitle is the current title of the Netscape window.
     Description: This topic will fail if Netscape can not provide both return values (a blank page is loaded, so there is no URL), or if an invalid dwWindowID is
     specified.
---

You can find the Netscape DDE API info at http://home.netscape.com/newsref/std/ddeapi.html

And, you can find some sample code at http://home.computer.net/~mheller/dde.html.

Hope this helps.
0
 
LVL 1

Author Comment

by:royster
Comment Utility
Thanks for your effort, tnatsni. I'm hoping there's a way to do it via an api call so I don't have to add code and hunt down info for each possible browser. - Roy
0
 
LVL 9

Accepted Solution

by:
Dalin earned 100 total points
Comment Utility
royster,
See if this works for you(it assume you use NetScape and the caption start with "Netscape", you can adjust)
Regards
Dalin

    Copy these code to a module:

    Option Explicit

    Public Type ProcData
        AppHwnd As Long
        title As String
        Placement As String
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type

    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As
    Long) As Long

    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As
    Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal
    nMaxCount As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As
    Long, lParam As Any) As Long

    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
    ' ***********************************************
    ' If this window is of the Edit class, return
    ' its contents. Otherwise search its children
    ' for an Edit object.
    ' ***********************************************
    Public Function EditInfo(window_hwnd As Long) As String
    Dim txt As String
    Dim buf As String
    Dim buflen As Long
    Dim child_hwnd As Long
    Dim children() As Long
    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)
         
        ' See if we found an Edit object.
        If buf = "Edit" Then
            EditInfo = WindowText(window_hwnd)
            Exit Function
        End If
         
        ' It's not an Edit object. Search the children.
        ' Make a list of the child windows.
        num_children = 0
        child_hwnd = GetWindow(window_hwnd, GW_CHILD)
        Do While child_hwnd <> 0
            num_children = num_children + 1
            ReDim Preserve children(1 To num_children)
            children(num_children) = child_hwnd
             
            child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
        Loop
         
        ' Get information on the child windows.
        For i = 1 To num_children
            txt = EditInfo(children(i))
            If txt <> "" Then Exit For
        Next i

        EditInfo = txt
    End Function
    ' ************************************************
    ' Return the text associated with the window.
    ' ************************************************
    Public Function WindowText(window_hwnd As Long) As String
    Dim txtlen As Long
    Dim txt As String

        WindowText = ""
        If window_hwnd = 0 Then Exit Function
         
        txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
        If txtlen = 0 Then Exit Function
         
        txtlen = txtlen + 1
        txt = Space$(txtlen)
        txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
        WindowText = Left$(txt, txtlen)
    End Function


    Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
    Dim buf As String * 1024
    Dim title As String
    Dim length As Long

        ' Get the window's title.
        length = GetWindowText(app_hwnd, buf, Len(buf))
        title = Left$(buf, length)

        ' See if the title ends with " - Netscape".
        If Left$(title, 8) = "Netscape" Then
            ' This is it. Find the ComboBox information.
            frmWindowList.lblAddress = EditInfo(app_hwnd)

            ' Stop searching.
            EnumProc = 0
        Else
            ' Continue searching til find it.
            EnumProc = 1
        End If
    End Function

    in form1, drop a label name it lblAddress, and a command button name it cmdFindAddress,
    add code:

    Option Explicit

    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
    ' Start the enumeration.

    Private Sub cmdFindAddress_Click()
        lblAddress.Caption = ""
        EnumWindows AddressOf EnumProc, 0
    End Sub
0
 
LVL 1

Author Comment

by:royster
Comment Utility
That works great =). Thanks a lot. I made a small modification to the code:
' See if the title ends with " - Netscape". (((replaced with:)))
' See if the title has a browser name in it
If InStr(title, "Internet Explorer") Or InStr(title, "Netscape") Then
' This is it. Find the ComboBox information.
Form1.lblAddress = EditInfo(app_hwnd)

Thanks again, Roy
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now