Solved

How to close  "Internet Explorer" using VC++

Posted on 2001-06-08
24
354 Views
Last Modified: 2010-04-02
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!
0
Comment
Question by:rinosh
[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
  • 9
  • 8
  • 2
  • +4
24 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 6167224
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.
0
 
LVL 1

Expert Comment

by:johnedwards2
ID: 6167225
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

0
 
LVL 6

Expert Comment

by:graham_k
ID: 6167231
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 );
     }

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6167462
Just to correct graham_k You should to compare if window title text start with "Internet Explorer" or "Netscape" not to be identical.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6167709
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6167799
The class in the above link will close all the internet explore windows with the following code:

std::vector<WindowPath> windowpath1;
windowpath1.push_back(WindowPath("IEFrame","",0));
for (;;)
{
     Ext_Wnd extPrg(windowpath1);
     if (!extPrg.m_hWnd) break;
     extPrg.CloseApp();
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 6167987
Axter, may I poit out that several equivalent solutions were already given in the comments? You should reconsider your behaviour...
0
 
LVL 30

Expert Comment

by:Axter
ID: 6168422
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6168436
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6168483
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?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6168498
>>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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6168667
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6169422
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();
0
 
LVL 30

Expert Comment

by:Axter
ID: 6169498
graham_k,
I'm sure if the questioner prefers your method, he will reject mine, and accept yours.
I have no problem with that.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6176106
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();
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6176167
for instance, from http://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
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6176202
To Axter !
Agree with graham_k. You need to see comment on posted link.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6177536
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.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6177906
"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 http://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


0
 
LVL 6

Expert Comment

by:graham_k
ID: 6180060
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.
0
 

Author Comment

by:rinosh
ID: 6189617
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 6190391
rinosh,
Did you try the class in the posted link?
It does exactly what you asked for with minimul coding on your part.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6199768
> 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();
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6401478
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:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

733 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