Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Listing/Closing all visible windows open on Windows OS

Posted on 2004-11-19
16
Medium Priority
?
220 Views
Last Modified: 2010-04-01
Hi there,

I come primarily from a Perl, PHP, S/R scripting background, so I am a fairly decent programmer, and I have a little experience with C++. What I would like to do is the following (on a MS Windows environment):

1. Get a list of all visible windows (either handles or window titles is fine)
2. If possible, I would also like to do the following:
        -Compile my program a consoleless (i.e. in a hidden window, so no console pops up)
        -Have the option to close all the windows open. I would like this to happen not by forcibly killing
         the process, but by 'politely' asking the window to close, so that if work has not been saved, the program
         will prompt the user to save data.

I am not really sure where to start with this in C++. I have written a program in Perl that does part of this (gets a list of all open visible windows) and compiled it as an EXE, but it is somewhat slower and larger in file size than I think a C++ exe would be.

Could someone help me get started with this? Like I said I am not terribly experienced in C++, but I should be pretty to teach. BTW, I am using the Dev-C++ compiler (not Visual C++).

Thanks in advance for your help,
Matt
0
Comment
Question by:nicholassolutions
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:Skonen
ID: 12631446
To get the windows handles, use EnumWindows:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/enumwindows.asp

Then to close a window from which you have the handle, send the SC_CLOSE system message to it:
::SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);
0
 
LVL 4

Expert Comment

by:Skonen
ID: 12631452
Keep in mind with SC_CLOSE, it's as though the user closed the application with the 'X', so there is no guarantee that the application will display a confirmation dialog.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12631913
Check out http://support.microsoft.com/kb/q175030/ ("How To Enumerate Applications Using Win32 APIs") and http://support.microsoft.com/default.aspx?scid=KB;en-us;178893& ("How To Terminate an Application "Cleanly" in Win32"). All these APIs should be available on CE also.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Author Comment

by:nicholassolutions
ID: 12632238
Thanks to both of you for the help -- unfortunately, I'm still a little confused, owing to my lack of C++ knowledge...Below is my feeble attempt to put this all together:

------------------------------------------------------------------------------------------------
 #include <windows.h>
 
 HWND hwnd;
 LPSTR wintitle;
 
 BOOL CALLBACK CloseWins(HWND hwnd, LPSTR wintitle, LPARAM lParam);

 BOOL CALLBACK CloseWins(HWND hwnd, LPSTR wintitle, LPARAM lParam) {
    GetWindowText(hwnd, wintitle, 200);
   
    if (wintitle != "Program Manager"){
      SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
      return true;
    }
    else{
      return false;
    }
 }

 int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance, LPSTR lpCmdLine, int cmdShow) {
                     
   EnumWindows(CloseWins, 0);
   return 0;
   
 }
--------------------------------------------------------------------------------------------

I get the following error messages from my compiler:
 
[Warning] In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*,:
Line 23: invalid conversion from `BOOL (*)(HWND__*, CHAR*, long int)'
[Build Error]  [Untitled1.o] Error 1

Line 23 is:  EnumWindows(CloseWins, 0);


I am guessing that I am making mistakes with passing pointers instead of values or vise versa (something I still make mistakes with sometimes), but I've tried it out a few ways with no luck (I posted this code because it had the fewest compilation errors, but I am guessing it is actually not the closest to correct). Could you guys help me find where the problem is?

Thanks again for your help!
-Matt


0
 
LVL 15

Author Comment

by:nicholassolutions
ID: 12632239
I should probably have said, "...where the problem*S* *ARE*..." :)
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 12767832
Closing the windows will not necessarily stop the processes.  Do you want to only close the windows or actually stop the processes as well?  I'm not very handy using C++ but I do have a routine to shut down designated windows AND processes should they be open (dll query).
0
 
LVL 86

Expert Comment

by:jkr
ID: 12767868
>>BOOL CALLBACK CloseWins(HWND hwnd, LPSTR wintitle, LPARAM lParam);

Um, that should be

BOOL CALLBACK CloseWins(HWND hwnd, LPARAM lParam);
0
 
LVL 15

Author Comment

by:nicholassolutions
ID: 12768589
I just want to close the windows, not kill the processes.

jkr -- I made that change, this compiles now, but now I have no method of checking the window names (to tell it not to close the desktop window. As a result, it tries to close the program manager, and I get a Windows shutdown menu (like pressing ALT+F4 on the desktop). How can I go about checking the window names correctly?

Thanks again,
Matt
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 12975879
To check the open window names you will need to make a dll query for all open windows.  Then you should specifically identify which windows NOT to close.  That way, regardless of which other ones are open, only those will be closed.
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 13190598
I still think the dll query would have helped.
0
 
LVL 15

Author Comment

by:nicholassolutions
ID: 13190675
>>I still think the dll query would have helped.
Sorry for not getting back to this question for a while -- I actually ended up solving my problem using a dll query using AutoIT (www.hiddensoft.com), and forgot about this question once the problem was solved. I'm still not quite sure how to do this in C++...if anyone could show me I'd still be interested on an 'intellectual' basis, and more than happy to award some points.
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 13190727
Glad to help.  Not really sure about C++.  I did this once using WISE.
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 13234064
Objection:

My comment - "I still think the dll query would have helped."

Author comment - "I actually ended up solving my problem using a dll query using AutoIT"

Doesn't this deserve some points?
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13263624
PAQed with points refunded (500)

modulo
Community Support Moderator
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.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

810 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