Click here to Skip to main content
15,860,844 members
Articles / Desktop Programming / Windows Forms

Getting Current Browser URL with VB.NET

Rate me:
Please Sign up or sign in to vote.
4.46/5 (11 votes)
27 Jul 2011CPOL2 min read 139K   12.6K   26   29
Get browser URL

Introduction

This article explains how you can get the current URL from your browser with VB.NET. The demo program demonstrates this for Windows Internet Explorer and Google Chrome.

Background

For one of my VB.NET programs, I wanted to get the current browser URL; of course I can copy and paste it, but if the program can get it for me, all the better. So I searched the internet and found numerous "solutions" but they were all either written in C or did not work (at least not for me). After reading various forums, I decided to adapt one of these solutions and managed to get the URL from Internet Explorer and from Chrome; attempts to get the URL from Firefox were unsuccessful.

My development configuration is: Microsoft Visual Basic 2008 Express, Windows7 Home Premium 64 bits, IE9, Chrome 11.0.696.68, Firefox 4. The code has also been tested on Vista 32 bits. The basic ideas and forum suggestions are referenced in the code.

The code is in the CurrentUrl.vb module, which you can add to your project. In your code, you should first verify that the browser (Internet Explorer or Chrome) is available.

For example:

VB.NET
...
Dim appName As String = "iexplore"
Dim proc As System.Diagnostics.Process = GetBrowser(appName)
...
Private Function GetBrowser(ByVal appName) As System.Diagnostics.Process
    Dim pList() As System.Diagnostics.Process =  
 System.Diagnostics.Process.GetProcessesByName(appName)
    For Each proc As System.Diagnostics.Process In pList
        If proc.ProcessName = appName Then
            Return proc
        End If
    Next
    Return Nothing
End Function
...	

If the browser process is returned, call the GetCurrentUrl function in CurrentUrl.vb with these parameters:

  • The browser window handle: proc.MainWindowHandle
  • The browser name: "Windows Internet Explorer" or "Google Chrome"
  • The classname of the window to look for: "Edit" for IE, "Chrome_AutocompleteEditView" for Chrome
  • (Optional) A combobox to get a "treeview" of the browser windows up to the target window; pass Nothing if you don't want this
VB.NET
...
If proc IsNot Nothing Then
    Dim browserName as string = "Windows Internet Explorer"
    Dim className as String = "Edit" 
    Dim s As String = 
GetCurrentUrl(proc.MainWindowHandle, browserName, className, ComboBox1)
    If s <> "" Then
        TextBox1.Text = s
        ComboBox1.SelectedIndex = 0 'Window list
    Else
        Label1.Text = "Current URL is not available"
    End If
Else
    Label1.Text = browserName & " is not available"
end If
...

Inside CurrentUrl.vb

This module contains a simple overview, references to the sources on which it is based, and definitions of the required Windows functions and constants, as well as some private variables.

GetCurrentUrl is the only public function; it calls the private EnumWindows function to get window handles until it finds the browser window and then look for the child window with the URL. The URL string is returned to your program.

Points of Interest

In the referenced sources, the search for the target window starts from the top window (Intptr.Zero) and it returns a list of URLs. My approach starts from the browser window (proc.MainWindowHandle); I used GetBrowser and debugged CurrentUrl until I found the target window with the appropriate classname to get only one URL. Unfortunately, this does not work for Firefox, which requires a completely different approach to get window handles (see Firefox Access).

History

  • May 2011 - First release
  • June 2011 - Source updated with Option Strict On

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot getting IE active URL. Pin
Priyanka Bhawsar18-Jun-14 21:00
Priyanka Bhawsar18-Jun-14 21:00 
AnswerRe: Not getting IE active URL. Pin
hvrmln19-Jun-14 7:30
hvrmln19-Jun-14 7:30 
GeneralMy vote of 3 Pin
Nataraj.P2-Sep-13 5:47
Nataraj.P2-Sep-13 5:47 
GeneralRe: My vote of 3 Pin
hvrmln5-Sep-13 3:44
hvrmln5-Sep-13 3:44 
Questiononly internet explorer worked Pin
kubilayyalcin18-Nov-12 15:07
kubilayyalcin18-Nov-12 15:07 
AnswerRe: only internet explorer worked Pin
Marc Koutzarov19-Nov-12 8:07
professionalMarc Koutzarov19-Nov-12 8:07 
GeneralRe: only internet explorer worked Pin
KNA925-Dec-12 5:29
KNA925-Dec-12 5:29 
GeneralRe: only internet explorer worked Pin
Marc Koutzarov10-Dec-12 17:14
professionalMarc Koutzarov10-Dec-12 17:14 
GeneralRe: only internet explorer worked Pin
Sumit Prakash Sharma18-Dec-12 20:39
professionalSumit Prakash Sharma18-Dec-12 20:39 
GeneralRe: only internet explorer worked Pin
Noobs R Us25-Oct-16 13:23
professionalNoobs R Us25-Oct-16 13:23 
SuggestionOptimised your code Pin
Marc Koutzarov16-Nov-12 0:57
professionalMarc Koutzarov16-Nov-12 0:57 
GeneralRe: Optimised your code Pin
hvrmln19-Dec-12 21:27
hvrmln19-Dec-12 21:27 
GeneralRe: Optimised your code Pin
Arciel23-Apr-14 17:15
Arciel23-Apr-14 17:15 
QuestionThank you :) Pin
Mohammad Arafat Fatafta8-Nov-12 9:16
Mohammad Arafat Fatafta8-Nov-12 9:16 
AnswerRe: Thank you :) Pin
hvrmln8-Nov-12 23:18
hvrmln8-Nov-12 23:18 
AnswerRe: Thank you :) Pin
Marc Koutzarov18-Nov-12 2:06
professionalMarc Koutzarov18-Nov-12 2:06 
QuestionAny update? Pin
Aldrin Jay Morales29-Oct-12 21:57
Aldrin Jay Morales29-Oct-12 21:57 
AnswerRe: Any update? Pin
hvrmln29-Oct-12 22:26
hvrmln29-Oct-12 22:26 
Questionhi how to download this article can't download proper please help me .... Pin
ganasavee23-Jan-12 23:39
ganasavee23-Jan-12 23:39 
AnswerRe: hi how to download this article can't download proper please help me .... Pin
ganasavee23-Jan-12 23:39
ganasavee23-Jan-12 23:39 
AnswerRe: hi how to download this article can't download proper please help me .... Pin
hvrmln24-Jan-12 0:53
hvrmln24-Jan-12 0:53 
QuestionWhere's the original C/C++ code...? Pin
jjjmail23-Aug-11 15:04
jjjmail23-Aug-11 15:04 
AnswerRe: Where's the original C/C++ code...? Pin
hvrmln24-Aug-11 23:26
hvrmln24-Aug-11 23:26 
QuestionCompiler errors - option strict Pin
craig185025-Jul-11 3:15
craig185025-Jul-11 3:15 
AnswerRe: Compiler errors - option strict Pin
hvrmln25-Jul-11 9:56
hvrmln25-Jul-11 9:56 
The version you have downloaded does not work with option strict on. I did not try this, but have now built a new version (Visual Basic 2010 Express) that does work with option strict on. The solution is quite simple: replace the errors with CTYPE(..., IntPtr), e.g.
....
child_hwnd = GetWindow(window_hwnd, CType(GW_CHILD, IntPtr))
If window_hwnd = CType(0, IntPtr) Then Return "" 'Exit Function
'Get the size of the window text
txtlen = SendMessage(window_hwnd, CType(WM_GETTEXTLENGTH, IntPtr), CType(0, IntPtr), CType(0, IntPtr))
....

I'll try to post the new version asap.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.