1. Computing
Restrict Internet access
Here's how to close an instance of Internet Explorer, if you find the current URL "inappropriate" (after retrieving the full URL of all opened Internet Explorer windows) - using only Delphi code!
 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
• Download source code
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Get current URL from IE
• Implementing Callbacks
• Windows API
• Message Handling

Back in the "Get current URL from IE" article, I presented you with one approach you can use to grab the URL from all opened IE windows using Delphi.

Suppose we are now interested in closing a particular IE window (that displays a specific document), or even shutting down all opened IE instances provided with the "not_allowed_to_browse_to" URL masks.

Before we proceed, recall that while retrieving the URLs of all IE windows, in the "Get current URL from IE" article, we added the Handle of every IE window to each ListBox item, along with the current web document address - URL.

To follow on, drop 3 new components onto our sample application: one TListBox (Name: lbNOK) and two new TButton components (Name : btnCloseIE, Caption : 'Close IE Window'; Name : btnCloseMask; Caption : 'Close with Mask'). The lbNOK list box should list all the partial URLs you do not want to allow in IE.

Restrict URL access with Delphi

   Close IE window
In order to close an external application from Delphi, we can send a specific message to a window provided by the window handle. Note that each window (or better to say windowed control), can be uniquely identified by its handle - an integer number - assigned by Windows.
In Delphi, this message sending operation, would look like:

PostMessage(AHandle, WM_CLOSE, 0, 0);

The PostMessage Windows API function is used to send a message to a control (in our case this control is IE's main window) but allows the control to wait until it has finished any other messages before it handles ours. The PostMessage's first parameter is the handle to the control we want to send a message; the second parameter is the actual message to be sent - the WM_CLOSE indicates a signal that a window or an application should terminate. The third and fourth parameters specify additional message-specific information (in our case no specific information is required).

All in all, this is how the OnClick event handler for the btnCloseIE button should look:

procedure TForm1.btnCloseIEClick(Sender: TObject);
var  IEHandle : THandle;
begin
  if lbIEURL.ItemIndex = -1 then
  begin
    ShowMessage('Please select an IE window by URL from the list!');
    Exit;
  end;
  IEHandle := THandle(lbIEURL.Items.Objects[lbIEURL.ItemIndex]);
  PostMessage(IEHandle, WM_CLOSE, 0, 0);
end;

Pretty easy, wouldn't you say? Note that in order to "save" the Handle, we have used the AddObject method of the TStrings object (Items property of the TListBox).

   Close with Mask
Another interested extension to this idea is to specify a list of (partial, masked) URLs, and, with one click, close all IE windows with the URL that matches the masks specified!

Here's the code:

procedure TForm1.btnCloseMaskClick(Sender: TObject);
var
  i,j : integer;
  Handle : THandle;
begin
  for i := 0 to -1 + lbIEURL.Items.Count do
  begin
    for j := 0 to -1 + lbNOK.Items.Count do
      if MatchesMask(lbIEURL.Items[i], '*' + lbNOK.Items[j] + '*') then
      begin
        Handle := THandle(lbIEURL.Items.Objects[i]);
        PostMessage(Handle, WM_CLOSE, 0, 0);
      end;
  end;
end;

Once again, it's a piece of cake using Delphi. The above code simply iterates through all the URLs found (in the previous call to btnRefreshClick procedure), if the URL matches the masks (using the MatchesMask RTL function) specified in the lbNOK listbox we send the WM_CLOSE message.

   Ideas? Questions?
That's it. If you have any questions be sure to ask on the Delphi Programming Forum.
Of course, do not forget to download the code from this article.

©2013 About.com. All rights reserved.