Thanks to Thomas Heller and Roel Schroeven for the help!

Here's a summary for a method I used to close an application from another, knowing only the process ID. The method is not ideal because the running child might have some dialog open. Ideally, some IPC mechanism should be used to request closing the target application. But, the method is can help under some circumstances.

To terminate a GUI application from its process ID, one way would be to:

- use win32gui.EnumWindows() to enumerate all windows in the system
- for each window handle found, check if the process ID match the requested on by using win32process.GetWindowThreadProcess() to get the process ID of each enumerated window.
- Check the window name of each enumerated window of the specified process ID using win32gui.GetWindowText(). You do not want to close a program that has a dialog open.
- Once you have the main window handle of the application and you know that there are no dialog opened in the application, close the application by posting WM_CLOSE (0x10) with win32gui.PostMessage(window_handle, WM_CLOSE,0,0).


Pierre Rouleau

_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to