Link to home
Start Free TrialLog in
Avatar of rinosh
rinosh

asked on

How to close "Internet Explorer" using VC++

Hi,

I am using VC++ application for calculating the browsing time of a system.

What I need is that if a user closes my application all the windows of "Internet Explorer" and "Netscape Navigator" should get closed.

If u can help me with some code, it will b beneficial.

Thanks!
Avatar of graham_k
graham_k

well, you probably want to use EnumWindows() described at  http://msdn.microsoft.com/library/psdk/winui/windows_7wvn.htm

you pass it a function which it should call once for each window on the system.  When it is called, you can test the window's title bar text and send a WM_CLOSE message if MSIE or Netscape.

I wrote a program to close annoying popup windows. The user can specify a text string which should in the window title & it will be closed. You can D/L the program with full C++ source and Borland C++ Builder project from

http://www.keellings.com/graham/software/download/yawn.zip

let me know if you need more help.
First you need the process id of the webbrowser application, see...
http://support.microsoft.com/support/kb/articles/Q175/0/30.ASP

Then kill it of via the method described in...
http://support.microsoft.com/support/kb/articles/Q178/8/93.ASP

for teh record, in case anyone buys this as a PAQ, teh relevant parts of the callback look something like this ...

  // Ask windows for a list of windows...
  EnumWindows( (WNDENUMPROC) ListWindowsCallback, (LPARAM) this);



bool CALLBACK TmainForm::ListWindowsCallback(HWND theHandle, LPARAM theParam)
{
  // theTitle is a string to receive teh window's title bar text
  if ((GetWindowText( theHandle, theTitle, sizeof(theTitle) - 1) == 0)

   // ok, we got the text. Compare it with "Internet Explorer" and "Netscape" and, if matched, do this ...
   

      if (PostMessage(theHandle, WM_CLOSE, 0, 0) == 0)
      {
        // should never happen, if it does, then call
        // GetLastError() for more info
        MessageBox( NULL,
                    "Contact program author",
                    "PostMessage failed !! ",
                     MB_OK|MB_ICONINFORMATION );
     }

Just to correct graham_k You should to compare if window title text start with "Internet Explorer" or "Netscape" not to be identical.
Avatar of Axter
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Axter, may I poit out that several equivalent solutions were already given in the comments? You should reconsider your behaviour...
jkr,
Please point out what method looks similar to mine.
Did you look at the class, and what it's doing.

The previous suggested method uses  EnumWindows.
My code does not use  EnumWindows.

If I've overlooked something, please feel free to point it out with a specific reference.
jkr,
>>You should reconsider your behaviour...
I would be more then happy to do so, if you would agree to help change the current policy in this topic area.

In fact, rather then waist this questioner's time, I'm going to post a zero point question here, concerning this matter.
I'm not stating that any method is similar to yours. I'm just saying that they're equivalent in what they achieve. BTW, that was the reason why I initially didn't even post a comment and was surprised to see that this Q got an 'answered' status. I wouldn't mind you locking that Q if you were the first one on this thread. But, you were clearly late.

How can you be sure that your method is so superior compared to the other ones?
>>How can you be sure that your method is so superior
>>compared to the other ones?

The others did not post a complete working method.

I posted a complete working method.  If the questioner drags and drops the code into their project, my code will close all IE windows.

That's the main difference between my answer and the previous comments.
If someone already had posted a complete working method, I would never post my comment as an answer.  I would have posted as a comment, as I ususally do.

Please see newly posted question.
well, just to drop my 2c worth. My code may not have been compelete in the sense that it won't compile as posted, but anyone how can't figure out what to do with it should not be contemplating writing code that closes applications (basically, it just needs a strcmp() and afew more close braces).

Also, mine doesn't rely on adding someone else's class. Personally, I prefer solutions that don't rely on other folks classes, libraries, DLLs, etc. maybe the questionner would have too?

Anyway, never mind, so long as the original poster finds something here that makes him happy. My archive gives some examples of how to sort through the list of open windows on the system, which some might find of interest.

best wishes,

~graham();
graham_k,
I'm sure if the questioner prefers your method, he will reject mine, and accept yours.
I have no problem with that.
Sure he will. I had thought, though, that you had taken the other stance in the moderator's question r.e answers v comments (guess I must have mixed you up with someone else. Sorry).

Have a nice day,

~graham();
for instance, from https://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20132273

Axter , "I cannot see any way that it could be justified that posting an answer as an answer is better for the
questioner."

But, before we start a flame war here, I'll post over there. Really, I'm not angry or annoyed (I point that our since one can't always tell in print). I just thought it a bit inconsistent.

As you say, the questioner will decide. And, as I always say - what's the good of these point thingies anyway? You can't buy beer with them and it's not as if EE ever sent anyone the promised T-shirt.

best wishes,

Graham
To Axter !
Agree with graham_k. You need to see comment on posted link.
graham_k,MadYugoslav,
I agree, and it would look real bad if I looked at this also.
But you have to understand the history behind this.

About two weeks ago, on a different question, a few of the C++ experts where discussing the proper method for posting an answer.
Myself and jhance try to convince the other C++ experts to use the post answers as comments.
When we failed to do so, we both decided to use their method.
I did so for about a week, I think I saw jhance do it for a few days.
Because I was not satisfied with the results, I have stop using this method, and have converted back to my previous post answers as comment method.

I apologize for posting this answer as an answer.
I would like to continue posting answers as comments, but  I don?t want to be one of the few top 15 experts using this method.
I hope you understand.
"I apologize for posting this answer as an answer". No need to apologize. You do what you do.

"I don?t want to be one of the few top 15 experts using this method." Guess that's why I'll never make the top 15, I don't have that competitive nature ;-)

I am and will remain a proponent of the "never post an answer" school of thought. However, I see your dilemma. Reminds me of Yossarian in Catch 22, when asked "but what if everyone thought that way?" .... "then I'd be a damned fool to think otherwise, wouldn't I?".

thanks for posting. No need for more. We can take it over to https://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20132273 if necessary.  

My take - as a mere mortal, I'll let the gods thrash it & ignore whatever they decide, carrying on with comments.

best wishes,

G


rinosh ,

  back on topic .... (almost - you said you wanted to do it in VC++)

There's a very elegent solution, again in Delphi on http://www.delphi3000.com/articles/article_1059.asp

I'm sure you can figure out how to convert it. It certainly appears worth it. This is quite slick.

Bascially, it's this ....

procedure TForm1.CloseBrowser(BrowserName:string);
var
  DDE:TDDEClientConv;
begin

DDE:=TDDEClientConv.Create(Self);
Try

// uncomment to close the last instance of Browser.

//      if DDE.SetLink(BrowserName,'WWW_Exit') then
//        if DDE.PokeData('anything' ,'anything') then
//            ShowMessage('Browser closed');
//
      While DDE.SetLink(BrowserName,'WWW_Exit') do
        if DDE.PokeData('anything' ,'anything') then
            ShowMessage('Browser was closed'); //This line is not necessary
Finally
      DDE.Free;
End;
end;

2. The call to the method must be accomplished with 'IExplore? for the Internet Explorer, 'Netscape? for the Netscape Communicator and 'Opera? for the Opera.
Avatar of rinosh

ASKER

Hi Graham
Thank u very much 4 ur Delphi code, but at present the system is designed in VC++, so I cant afford to do it in Delphi.

Anyway give me time to evalute the answer.

Thanks
Rinosh
rinosh,
Did you try the class in the posted link?
It does exactly what you asked for with minimul coding on your part.
> Hi Graham
Thank u very much 4 ur Delphi code, but at present the system is designed in VC++, so I cant afford
to do it in Delphi.

That's quite clear, but it ought to be simple to translate those few lines into c++.

good luck,

~graham();
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.

If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.

Your response in finalizing this (and ALL) your question(s) is appreciated.

Site-related HELP:  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
Moondancer
Community Support Moderator @ Experts Exchange