Getting Current Browser URL with VB.NET

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.

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).

Comments and Discussions

Hello Friends, do you need an urgent Xmas loan today i know of a kind and a God fearing Woman called Mrs. Stacey who can help you get a good loan asap contact now and be free of scan today email : (staceybuckley81@gmail.com) ..... (staceybuckley81@gmail.com) .

I tried this code but sometimes it is showing the active URL and sometimes not showing. So what should I have to do to get active URL from IE browser or there is any other issue through this I am unable to get url ?

I think this is a browser window focus issue. If the Url does not appear, click on the browser window and then try again. Sorry I have no yet been able to solve this.Thank you for your interest in this article.

Originally this program worked for both IE and Chrome; it never worked for Firefox or any other browser. Somehow it stopped working for Chrome but I did not change anything. Someone else managed to get it to work again for Chrome (see the other messages) but I never saw his solution and have not yet found a solution myself.

Hi great piece of code there. It really got to me. I spend a whole night playing with it.

I found a way you don't need a browser and a class-name. What still bothers me is the fire-fox issue. FF seems to have a total different way of working. In my code I also found the Entry-point en Base address of the browsers process.

You can send me an email and i will send the source-code if your interested.

You are right: this tool no longer works with Google Chrome; maybe that's because Chrome was recently updated (to version 23). I had not yet noticed this problem because I mostly use IE but will look into it and will let you know when it is solved -- if I can solve it.

As this tool is designed to get the current URL, it cannot get other URLs, nor can it open or close any tabs.

Thank you for your interest in my article. I have do not made any changes to the original post, so I do not know why you are experiencing this problem. The code works fine with Internet Exporer9 and Chrome on my systems (Windows7 and Vista).

I am unable to get your project to work with option strict on. Did you try the code with option strict on? Does anyone code with option strict on? I sure would like to use your code, a good alternative to referencing a VB6 win32 dll.

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.....

Also you need to be careful when you play with DLLImport. Remember that you are giving out a memmory handle to poppulate required details. so you need to maintain your unsafe object properly so that the memmory positions is intact when the call returns from non managed code