Listing/Closing all visible windows open on Windows OS

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,
LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To get the windows handles, use EnumWindows:

Then to close a window from which you have the handle, send the SC_CLOSE system message to it:
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.
Check out ("How To Enumerate Applications Using Win32 APIs") and;en-us;178893& ("How To Terminate an Application "Cleanly" in Win32"). All these APIs should be available on CE also.
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

nicholassolutionsAuthor Commented:
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;
      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!

nicholassolutionsAuthor Commented:
I should probably have said, "...where the problem*S* *ARE*..." :)
huntersvcsRetired ITCommented:
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).
>>BOOL CALLBACK CloseWins(HWND hwnd, LPSTR wintitle, LPARAM lParam);

Um, that should be

BOOL CALLBACK CloseWins(HWND hwnd, LPARAM lParam);
nicholassolutionsAuthor Commented:
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,
huntersvcsRetired ITCommented:
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.
huntersvcsRetired ITCommented:
I still think the dll query would have helped.
nicholassolutionsAuthor Commented:
>>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 (, 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.
huntersvcsRetired ITCommented:
Glad to help.  Not really sure about C++.  I did this once using WISE.
huntersvcsRetired ITCommented:

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?
PAQed with points refunded (500)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.