-
app kill
i have written a program that runs some system commands and then opens another app to do something.
The problem i have is that my program won't continue until the app is closed manually, but i am wriiting an automation program so this is a major problem.
I know the process name, whats the easiest way to shut it down.
Thanks
-
You could use FindWindow() and SendMessage to send a WM_QUIT message to the window...
Code:
#include <windows.h>
// ...
HWND hWindow = FindWindow(NULL, "Window Name");
SendMessage(hWindow, WM_QUIT, 0, 0);
If all else fails, there's always TerminateProcess()... look it up on MSDN.
-
thanks for the reply.
How do i get the window name, i take it this is not the same as the process name as this did not work.
Thanks
Shakespeare
-
No, it's not the same as the process name... just open the Process you wish to close and record the window title.
For example, if you wanted to close the Recycle Bin:
Code:
HWND hRecycle = FindWindow(NULL, "Recycle Bin");
if (hRecycle == NULL)
MessageBox(HWND_DESKTOP, "Failed ad FindWindow()!", "Error!", MB_ICONERROR);
-
I'd use ExitProcess before I used TerminateProcess
-
ExitProcess is they "safe" way of exiting a process whereas TerminateProcess will kill an app without taking any consideration to what killing an app may do. If you want to guarantee that an app will die and you know that doing so won't cause a memory leak I actually prefer using TerminatProcess.
-
I tried this on the recycle bin window:
Code:
HWND hRecycle = FindWindow(NULL, "Recycle Bin");
if (hRecycle == NULL)
MessageBox(HWND_DESKTOP, "fail", "Error!", MB_ICONERROR);
else
SendMessage(hRecycle, WM_QUIT, 0, 0);
it did not work, the recycle bin remained open. And i have also tried termainating the process but have been unable to get the exit code. it returned 0x00000000.
Thanks.
-
Try sending it a WM_CLOSE instead.
-
still doesn't work, any other ideas?
thanks for your help so far guys.
-
i forgot to say, that it definitley recognises whether the window is open or not. I just can't close it.
-
OK it works but not with window names with spaces in.
-
Perhaps you typed it wrong. I managed to close all sorts of windows using the method I described above.
-
yeah sorry, i didn't fully test it.
I tried it on the recycle bin and it didn't work. But i can close almost any other window and it works with the window i need to close so it is fine.
Strange how it won't close the recycle bin though.
Thanks for your help.