I'm trying to close a window (in C++). I need to do that without the use of FindWindow().
My app should find a window of another app (again , without FindWindow()) and close it .
I've written both apps (two different apps , not in the same .cpp file) , one for the window , and one for the app that needs to find the window (not all done of course) , but I can't seem to find a way to find the window without "FindWindow".
Interprocess communication. Basically have the owner of the window transmit to the other application the window handle, or better yet: Have the application that wants to close the window transmit a command to the other application so it can close its own window.
Personally, I think I would use RegisterWindowMessage() to settle on the message to send, then whenever needed, the closer app would broadcast the message and in response, the owner app would close the window.
You are willing to use all kinds of Win API functions in odd ways to find a window, but you are unwilling to use the Win API function designed to do exactly that?
Since my approach needs to have the source code for both apps, I don't mind. True that not using FindWindow() is just a major pain, but to each its own, I guess.
With my approach, you need to change both apps. The app creating TestWin needs to register a window message when it starts up, say in WinMain:
1 2 3 4 5 6 7
//Globals
const TCHAR c_WindowMessageName = TEXT("MyWindowMessage"); //Or whatever name you like.
UINT closeMsg;
....
//Inside WinMain()....
closeMsg = RegisterWindowMessage(c_WindowMessageName);
Now, inside your TestWin's window procedure, you process this message. For this to work, the TestWin window must be a top level window:
1 2 3
case closeMsg:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
Now the closer application just needs to register the same window message and you are good to go:
1 2 3 4 5 6 7 8 9 10 11 12
//Globals
const TCHAR c_WindowMessageName = TEXT("MyWindowMessage");
UINT closeMsg;
....
//Inside WinMain()....
closeMsg = RegisterWindowMessage(c_WindowMessageName);
....
//Send the close message whenever you want/need. You must broadcast it.
//PostMessage so the call is non-blocking.
PostMessage(HWND_BROADCAST, closeMsg, 0, NULL);
Please note that internally FindWindow() will send WM_GETTEXT message to every top-level window, and if one of them is hunging, your application will hung too. So please use it carefully.
Please note that internally FindWindow() will send WM_GETTEXT message to every top-level window, and if one of them is hunging, your application will hung too. So please use it carefully.
What?!!
You're telling us that the OS doesn't have a list of top level windows it can look through?
That the people at MS that designed the system were so clueless that they implemented a heavily-utilized function in such a clumsy and easy to break way?
webJose , after the second application has closed , how can the first application know that the second application has just been closed ?
One way that works for me is by checking before everything , that the Process-ID of the second application is legit (i.e. not ZERO) , and it works fine , however the code for checking the P_id is a little bit large . Can I know in some other way if the second app closed itself ?
everybody,really , thanks a lot (you're indeed the bests)
I provided a one-way interprocess communication method. Now you want a two-way. For this it would be better if you used something else, like a named pipe. Google for them as I won't explain there here because they are not the most trivial, nor will I write the code for you. If you don't like them, then look up for other forms of interprocess communcation and pick up the one you like best.