Solved

Closing Internet Explorer

Posted on 2000-05-01
5
416 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
[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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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