DelphiFAQ Home Search:

Closing Internet Explorer (IE) from Delphi

 

comments8 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

Question:

My application has to close the IE. How can an application close the Internet Explorer or an Explorer window?


Answer:

The key is to post to the *right* window. Use the code below and it will close all instances of IE.

program Sample;

function CloseIEs(Wnd : HWnd; Form : TForm1) : Boolean; export; stdcall;
var
  sCap : array [0..255] of char;
begin
  GetWindowText (Wnd, sCap, sizeof(sCap));
  if pos ('Microsoft Internet Explorer', sCap) > 0 then
  begin
    PostMessage (Wnd, WM_CLOSE, 0, 0);
  end
  else
  begin
    // check by class name!
    GetClassName (Wnd, sCap, sizeof(sCap));
    if sCap = 'IEFrame' then
      PostMessage (Wnd, WM_CLOSE, 0, 0);
  end;

  CloseIEs := true; { next window, please }
end;

begin
  // close all hidden instances
  EnumWindows(@CloseIEs, 0);
end.
You don't like the formatting? Check out SourceCoder then!

Comments:

2006-11-15, 08:36:31
hunter from China  
so good!
2008-08-26, 08:28:06
anonymous from Finland  
WARNING !

I have not tested this, but I suspect the code above has a dangerous bug - maybe it will close not only every instance of MS-Internet explorer, but also any TWebBrowser component that is on any form included in any Delphi -made program !

Since it is not normal to close individual components in a running Delphi -program, this may cause severe malfunction in any delphi-application affected.

especially I find this one suspicious:

begin
// check by class name!
GetClassName (Wnd, sCap, sizeof(sCap));
if sCap = 'IEFrame' then
PostMessage (Wnd, WM_CLOSE, 0, 0);
end;

Myself, I would use the EnumWindows API call instead.
it requires a callback function, but defining your callback with the stdcall directive should make that no problem at all.



2009-05-22, 05:32:42
anonymous from Belarus  
пиздец, нихуя не понятно
2009-05-22, 08:59:43
anonymous from United States  
Belarus, these articles are well beyond your tiny little brain, or your translator capability. Stop insulting authors here and go back to what you do best. http://ru.youtube.c..n4cS68FODo
2010-01-13, 15:49:40
anonymous from Moldova  
eewewewewe
2011-12-28, 05:33:44
Мишаня  
Внатуре заебленная хуета какаето
2013-11-01, 03:11:09   (updated: 2013-11-01, 03:11:42)
leveni from Potts Point, Australia  
rating
I've tried it and it works really well. Fantastic programming.
2014-02-17, 07:39:42
anonymous from Netherlands  
ignore that warning, works great even with twebbrowsers

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: