Solved

How to close  "Internet Explorer" using VC++

Posted on 2001-06-08
24
319 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
  • 9
  • 8
  • 2
  • +4
24 Comments
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:MadYugoslav
Comment Utility
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
Comment Utility
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:graham_k
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
To Axter !
Agree with graham_k. You need to see comment on posted link.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now