Sending WM_CLOSE message to a Window
-
Hi. I'd like to close an application by sending to its main window the WM_CLOSE message. What I do is getting the ID of the process I want to stop, and then I use EnumWindows() function to get all top-level windows in the screen and in the callback function EnumWindowCallBack(), I compare the process ID with the ID of each top-level window. When the ID is the same, I send to the window the WM_CLOSE message thanks to ::PostMessage(). But, unfortunately, it doesn't work all the times. It seems that the windows I get in the callback function EnumWindowCallBack() are not the main windows of the applications I want to stop... For example, with calc.exe, the window is closed after having posted the WM_CLOSE message twice but the process calc.exe is still running without a window... Has anyone already met this problem ? Is there a way to be sure to get the right window of an application (and not a child window that doesn't handle the WM_CLOSE message) ? Thanks.
-
Hi. I'd like to close an application by sending to its main window the WM_CLOSE message. What I do is getting the ID of the process I want to stop, and then I use EnumWindows() function to get all top-level windows in the screen and in the callback function EnumWindowCallBack(), I compare the process ID with the ID of each top-level window. When the ID is the same, I send to the window the WM_CLOSE message thanks to ::PostMessage(). But, unfortunately, it doesn't work all the times. It seems that the windows I get in the callback function EnumWindowCallBack() are not the main windows of the applications I want to stop... For example, with calc.exe, the window is closed after having posted the WM_CLOSE message twice but the process calc.exe is still running without a window... Has anyone already met this problem ? Is there a way to be sure to get the right window of an application (and not a child window that doesn't handle the WM_CLOSE message) ? Thanks.
Knowledge Base article Q178893 - "HOWTO: Terminate an Application 'Cleanly' in Win32" - deals with these issues. Basically, you should call WaitForSingleObject after posting WM_CLOSE. You can find the article online: http://support.microsoft.com/support/kb/articles/Q178/8/93.ASP Cheers Tomasz Sowinski http://www.shooltz.com.pl