1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Get current URL from IE
How to locate the control in Internet Explorer that has the current URL.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 2: The real CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Restrict URL access

• Implementing Callbacks
• Windows API
• Message Handling
 Elsewhere on the Web
• WinDowse

I often get asked questions like : "How do I get/retrieve the current URL / web document address from Internet Explorer?", "How do I hook to IE URL address bar?", "How to enumerate all IE windows and pick the web address?" and even "How do I intercept and change URL's in IE?"

This article presents one approach you can use to grab the URL from all opened IE windows using Delphi.

   IE Address Bar
In the Internet Explorer the current URL is displayed inside the combo box component on the Address bar. In order to "steal" the current URL from IE, you'll need to find the (control class) name of that combo box, locate it, and grab its text. Now, this seems easy, but how do you do that?

To be able to grab a text from an arbitrary window (and its child windowed controls) on the Windows Desktop, you'll need to play with Windows API calls and Windows Messages. First, or better to say last, when you find that combo box with the current URL in it, you can send a WM_GETTEXT message to that window and pick the text displayed inside. Second, or better to say first, you need to know the exact order of controls that are parents to that URL combo box. Then, you use the FindWindowEx API call in order to retrieve the handle (an integer number that uniquely identifies each control / window) of the child window. Ok, all seems easy, but again how do you know the structure of IEs controls and their parenthood?

Window information to the max
An easy way of finding the hierarchy of controls on any Windows program, is to first grab a (free) tool called WinDowse - an extremely convenient and easy to use tool for obtaining necessary technical information about any window. Place a mouse cursor on a window, and WinDowse will show all parameters of the window and window class including list of parent hierarchy.

Now you know how Delphi guru developers know everything about any application :)

WinDowse grabbing IE child windows class names
   Locating IE windows
So, now we know the internal structure of IE, and what that combo box (its class) carrying the URL is named (that is 'ComboBox'); and more importantly we know the names of its parents. Note that the main IE window is called "IEFrame" - this is like TApplication for any Delphi application.
To retrieve the handle to the top-level window whose class name is provided (in our case: IEFrame), you can use the FindWindow API call. When you have the IE handle you then use the FindWindowEx API, to locate that ComboBox control with the URL. Finally, as already mentioned above, you send that ComboBox the WM_GETTEXT message to obtain the text from it.

This seems easy. However, note that FindWindow API retrieves the handle to the top-level window. What this means is that if you have multiple instances of IE opened (as you probably have) the function will locate the last activated IE window. Uh, now what?

Windows help! Callback!
Finally, here's the solution: Windows function callbacks. Simply put, a callback function is a routine in your program that Windows calls. A good example of Windows API functions that require callback functions are enumeration functions, like EnumWindows. The EnumWindows function enumerates all top-level windows on the screen by passing the handle of each window. Ha, this is what we need!

Conclusion on how to retrieve the URL from all opened IE windows:

  1. Enumerate all top-level windows using the EnumWindows callback (this gives us a Handle)
  2. Grab the Caption (Title) of each Window
  3. Look only for windows whose class name is "IEFrame"
  4. Use the FindWindow provided with the Handle and the Caption to locate the correct IE window
  5. Call FindWindowEx several times to locate the ComboBox carying the URL, as it is the child to some parent which is a child to our top-level IEFrame (this is the class name) window
  6. Send a message to the ComboBox to retrieve the text inside it
  7. Nothing more, really.

I suppose you are more than eager to see the real code...

Next page > The real CODE > Page 1, 2

©2017 About.com. All rights reserved.