Solved

Close specific IE window

Posted on 2006-06-12
11
437 Views
Last Modified: 2010-04-05
Hi there

I am using this code to close a IE window by address

Procedure QuitBrowser( sUrl : string );
var
 ShellWindow: IShellWindows;
 WB: IWebbrowser2;
 spDisp: IDispatch;
 IDoc1: IHTMLDocument2;
 k: Integer;
begin
 ShellWindow := CoShellWindows.Create;
 // get the running instance of Internet Explorer
 for k := 0 to ShellWindow.Count do
 begin
   spDisp := ShellWindow.Item(k);
   if spDisp = nil then Continue;
   // QueryInterface determines if an interface can be used with an object
   spDisp.QueryInterface(iWebBrowser2, WB);
   if WB <> nil then
   begin

     WB.Document.QueryInterface(IHTMLDocument2, iDoc1);

     if iDoc1 <> nil then
     begin
       WB := ShellWindow.Item(k) as IWebbrowser2;
       If sUrl = WB.LocationURL then
     //  WB.Quit;  {method 1}
       PostMessage (WB.HWND, WM_CLOSE, 0, 0);

       end;
     end;
   end;
end;


As you can see I have tried WB.Quit and PostMessage (WB.HWND, WM_CLOSE, 0, 0);

Both of these seem to work AOK on my Window Xp home computer, however, I get an error
Access is denied when I run this on my windows 2000 computer.... I got no idea!

Any suggestions?
0
Comment
Question by:wildzero
  • 7
  • 3
11 Comments
 
LVL 10

Author Comment

by:wildzero
ID: 16884495
Also tried

    SendMessage(WB.HWND,WM_DESTROY,0,0);
    SendMessage(WB.HWND,WM_NCDESTROY,0,0);

but no lock there either
0
 
LVL 10

Author Comment

by:wildzero
ID: 16884521
SendMessage(WB.HWND, wm_Destroy, 0, 0);

that threw up an exception
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16890231
for k := 0 to ShellWindow.Count do  <-- I suppose it should be "to ShellWindow.Count - 1"
0
 
LVL 10

Author Comment

by:wildzero
ID: 16890716
true, i changed it but still fails...
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16894210
WB.Quit worked fine for me but I am using XP, too.
When my buddy comes online later I'll get him to try on Win2K.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Accepted Solution

by:
EddieShipman earned 250 total points
ID: 16896094
Her just tried it and it worked for him, too.

did you try it this way:

var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  k: Integer;
  sURL: String;
begin
  ShellWindow := CoShellWindows.Create;
  for k := 0 to ShellWindow.Count-1 do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then
      Continue;
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        // MUST strip the protocol (http://) and the
        // path off the URL, first
        if iDoc1.domain = sURL then
          WB.Quit;  {method 1}
      end;
    end; {if WB}
  end; {for}
end;
0
 
LVL 10

Author Comment

by:wildzero
ID: 16897144
Cool I will give that a go
Maybe my Windows2000 has had something to stop IE windows being closed by another application.
Ah well
will keep you informed.
0
 
LVL 10

Author Comment

by:wildzero
ID: 16910263
Hmm tired that and still got an exception error.
Herm
Works on on my Windows XP machine but not my 2000 Pro machine.
:-/
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16911249
Could be permissions issue, otherwise I have no idea.
Did you try running it under a user with Admin rights?
0
 
LVL 10

Author Comment

by:wildzero
ID: 16914846
The user I run it under is the only user on the computer ;-)
The windows 2000 install is well comming up to 5 years old so I bet the system is a little messed up (still runs rock solid though, after 3 hardware upgrades) - to bad windows xp isn't as stable hehe.

Might see if I can find some other people out there running 2000 and see if they can test it for me.

Thanks!
0
 
LVL 10

Author Comment

by:wildzero
ID: 16925267
Thanks :D
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now