Link to home
Start Free TrialLog in
Avatar of marco_coder
marco_coder

asked on

autoclose all open windows c/c++ or masm32.

Hi,

I am looking for some code to CLOSE ALL windows. (not shutdown winxp)


- find all OPEN windows  AND/OR find by Title,caption
- close it after 20 seconds. (repeat forever :-) )



Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

C++:

1) The GetDesktopWindow function returns a handle to the desktop window.

2) The EnumWindows function enumerates all top-level windows on the screen.

3) For each top-level window call EnumChildWindows. This call should be recursive to get all childs (call the same function for each enumerated window).

- AlexFM (http:/Q_20543904.html)
..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);
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of marco_coder
marco_coder

ASKER

Hi,

so far i have managed to FIND the window.
And i try`d to close it but....
ERROR CODE =5 WICH = ACCESS DENIED.

see code here:
-------------------
#include <windows.h>
#include <iostream>
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")



using namespace std;
void blahblah();
int Message();


int main(int argc, TCHAR* argv[], TCHAR* envp[])
{

blahblah();

}



void blahblah()
{


      DWORD SEED=0;
      HWND hWin;
HWND hWin2;
const DWORD MINOR=30;
   for(;;)
   {

         if( hWin = FindWindow(NULL,"Million Dollar Traffic") )
{
cout << endl << " window  found" << endl; //TApplication
SendMessage(hWin,WM_QUIT,(LPARAM)0,(WPARAM)0);
//SendMessage(hWin,WM_DESTROY,(LPARAM)0,(WPARAM)0);
//SendMessage(hWin,WM_CLOSE,(LPARAM)0,(WPARAM)0);
//CloseWindow(hWin);
if(DestroyWindow(hWin)==FALSE)
Message();
}
//Internet Explorer_Server




Sleep(1000* (SEED+MINOR));
   SEED+2;
   SEED*5 /MINOR;
if(SEED>20)
SEED=1;


   } // end for


}



int Message()
{
LPVOID lpMsgBuf;
if (!FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL ))
{
   // Handle the error.
   return 1;
}

//MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
cout << endl << (LPCTSTR)lpMsgBuf << endl;
// Free the buffer.
LocalFree( lpMsgBuf );





return 0;

}





---------------------------


i know its complex.... i wonder how i can get ACCESS to a window instead of ACCESS DENIED.... confused..!

it worked!
Thanx!
you only specify windows title

CWnd* pWnd = CWnd::FindWindow(NULL, "india");
if (pWnd)
{
MessageBox ("found");
pWnd->GetOwner()->PostMessage(WM_QUIT);
}

here india is my application title