1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/od/windowsshellapi/l/aa060303b.htm

was emailed to:

Thanks for sharing About.com with others!

Get current URL from IE
Locate IE, find the AddressBar, pick the URL text!
 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 1: IE Address Bar
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Restrict URL access

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

All set, we know the theory, let's proceed to the Delphi code.
Here's how to build a sample project able to retrieve the URL text from all opened Internet Explorer instances.

Start Delphi, and on that default new form place a TListBox (Name: lbIEURL) and a TButton (Name : btnRefresh; Caption : 'Find IE windows').

Delphi grabbing IE URLs

We go step by step:

1. Enumerate all top-level windows using the EnumWindows callback (this gives us a Handle)
Let the btnRefresh OnClick event handler look like:

procedure TForm1.btnRefreshClick(Sender: TObject);
begin
 lbIEURL.Clear;
 EnumWindows(@GetUrlFromIE, LParam(lbIEURL.Items));
end;

Note: if you are unfamiliar with the ideas of working with callback functions, please read the "Hey Windows, Call me!" article.

The GetUrlFromIE function is declared in the interface section as
function GetUrlFromIE (Handle: THandle; List: TStringList): boolean; stdcall;
This function is an application-defined callback function that receives top-level window handles as a result of a call to the EnumWindows.

Look inside the GetUrlFromIE function...

2. Grab the Caption 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

function GetUrlFromIE (Handle: THandle; List: TStringList):
 boolean; stdcall;
var
  hWndIE, hWndIEChild : HWND;
  Buffer : array[0..255] of Char;
begin
  //get the window caption
  SendMessage(Handle, WM_GETTEXT, 255, integer(@Buffer[0]));
  //look for the Internet Explorer window with "Buffer" caption
  hWndIE := FindWindow('IEFrame', Buffer);
  if hWndIE > 0 then

(* continues below... *)

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. Add the URL to the StringList object provided in the GetUrlFromIE as a parameter

  begin
    //try to get a handle to IE's toolbar container
    hWndIEChild := FindWindowEx(hWndIE, 0, 'WorkerW', nil);
    if hWndIEChild > 0 then
    begin
      //get a handle to address bar
      hWndIEChild := FindWindowEx(hWndIEChild, 0, 'ReBarWindow32', nil);
      if hWndIEChild > 0 then
      begin
        //finally, locate combo box and add its text to the list
        hWndIEChild := FindWindowEx(hWndIEChild, 0, 'ComboBoxEx32', nil);
        if hWndIEChild > 0 then
        begin
          SendMessage(hWndIEChild, WM_GETTEXT, 255, integer(@Buffer));
          //List.AddObject(Buffer,TObject(hWndIE));
          List.Add(Buffer)
        end;
      end;
    end;
  end;
  //continue enumeration
  Result :=True;
end; (*GetUrlFromIE*)

8. Nothing more, really.

That's it. You now have the TListBox component filled with the URL addresses from each IE window!

   Moving forward...
Look once again inside the GetUrlFromIE function, notice the commented line:
List.AddObject(Buffer,TObject(hWndIE));
If you uncomment this one and replace it with the List.Add(..) above, you will place the Handle to the IE window in the List property of the ListBox.

So what to do with that Handle?
When we have the Handle to the window we can easily close it, for example (yes, close an external application from Delphi)! Or, what do you say about changing the URL address (if you find it unappropriate) and sending the [Enter] key to the IE (that ComboBox)? Need to restrict the access (to users running your application) to some URLs?

Stay tuned! In the following articles you'll see how to extend this code to the max!

  • Restrict Internet access
    Here's how to close an instance of Internet Explorer, if you find the current URL not to be "appropriate" (after retrieving the full URL of all opened Internet Explorer windows) - only using Delphi code!

    First page > How to locate the control in IE that has the current URL. > Page 1, 2

  • ©2014 About.com. All rights reserved.