Solved

Close specific IE window

Posted on 2006-06-12
11
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

627 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