Solved

Closing Internet Explorer

Posted on 2000-05-01
5
415 Views
Last Modified: 2010-04-04
I am trying to close any open window of Microsoft Internet Explorer.  As far as I know the class of this window is 'IEFrame', so the folowing code should do it:

procedure TForm1.StopInternetExplorer;
var WinHandle: Integer;
begin
   WinHandle := FindWindow('IEFrame', nil);
   if WinHandle <> 0 then SendMessage(WinHandle, wm_close, 0, 0);
end;

The problem is that it doesn't work (I close other windows with this code).  Even using the windows caption instead of the class doen't work.  In both cases you can hear a system beep and that's all.

Any ideas
0
Comment
Question by:clopez
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 2767325
either of these will work:

procedure CloseIExplorer(h : HWND);
Var pID,hProcess:DWord;
Begin
GetWindowThreadProcessId(h,@pID);
hProcess:=OpenProcess(PROCESS_TERMINATE,TRUE,pID);
TerminateProcess(hProcess,1);
End;


procedure TForm1.Button1Click(Sender: TObject);
Var
 IExplorer : Thandle;
begin
 IExplorer := FindWindow('IEFrame',nil);
 If IExplorer <> 0 Then
CloseIExplorer(IExplorer);
 end;


procedure TForm1.Button2Click(Sender: TObject);
Var
 IExplorer : Thandle;
begin
 IExplorer := FindWindow('IEFrame',nil);
 If IExplorer <> 0 Then
 SendMessage(IExplorer, WM_SYSCOMMAND,SC_CLOSE,0);
end;
0
 
LVL 17

Expert Comment

by:inthe
ID: 2767333
button2 click code will suffice just showing also how to use terminateprocess.

wm_close/wm_quit dont always work therefore better to use
WM_SYSCOMMAND,SC_CLOSE as this always works.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2768230
Oh Barry, once again you forget to close a handle...   :-(

I prefer using WM_CLOSE as long as this works alright, because TerminateProcess may (seldomly) leave system instabilities. I'm closing IE with WM_CLOSE and it works perfectly.

(1) You have to find the correct processID of the IE. You can use Barry's code (FindWindow + GetWindowThreadProcessID) or you can enumerate all running processes and look for "IExplorer.exe" (see my homepage for a free unit that solves this problem with D4+D5).
(2) Once you have the processID, you should call EnumWindows and check all windows that belong to the explorer (by calling GetWindowThreadProcessID for each enumerated window and matching this processID with the processID from step 1).
(3) Now you get all explorer windows. But you should only post WM_CLOSE to the main windows (the windows that are visible on the taskbar). You can check that with a number of API calls. But easier would be to use my free unit again.
(4) Use PostMessage instead of SendMessage.

Regards, Madshi.

http://beam.to/madshi
0
 

Author Comment

by:clopez
ID: 2768509
Thanks indeed for your comments, I'll take a look to your page Madshi
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2768530
The unit is "enumStuff.pas".
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now