Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Closing Internet Explorer

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
clopez
Asked:
clopez
  • 2
  • 2
1 Solution
 
intheCommented:
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
 
intheCommented:
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
 
MadshiCommented:
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
 
clopezAuthor Commented:
Thanks indeed for your comments, I'll take a look to your page Madshi
0
 
MadshiCommented:
The unit is "enumStuff.pas".
0

Featured Post

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now