Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A kinder way than TerminateProcess?

I have a backup app that will close user-defined running programs before the backup so that open data files can be closed and flushed before the backup. After the backup is complete it restarts the programs in the list.

I have no problem getting the window Handle using the Caption and PostMessage(AppHandle,WM_CLOSE,0,0); That works fine for most apps, but not for ones running in the Notification Area (System Tray)

Currently I am using TerminateProcess( and it works for those Notification Area apps, but it leaves files open as Windows bypasses any Close instructions and just slams those apps down.

I have searched long and hard and I cannot find a nicer way to shut down Notification Area apps. Can anyone please help?

Thanks

like image 693
user568160 Avatar asked Jun 17 '11 22:06

user568160


2 Answers

In order to close a program gracefully, you need to know something about how that program expects to be closed. If closing the main window accomplishes it, then you need to know how to recognize the "main" window.

Programs don't run "in" the notification area. They display icons there. Any program with a notification icon must also have a window (because the shell tells the program that the icon has been clicked by sending messages to the window). Even if the window isn't visible, it still must exist. If you can determine some set of properties that identify the window associated with a particular notification icon, then you could close it. There is no standard set of attributes to look for, though; each program can do it differently.

And even if you find the window you're looking for, closing it might not be the way the program expects to be terminated, either. It might expect a certain command from the notification icon's menu instead, or some message sent by a dialog box that the program displays.

like image 104
Rob Kennedy Avatar answered Oct 08 '22 21:10

Rob Kennedy


If your application is running on Windows Vista or Windows 7, don't shut down programs - instead use the Volume Shadow Copy service to access a snapshot of the files while they are still in use. This is what the Windows 7 built-in backup program does.

If you're on an earlier version of Windows, there's no foolproof solution. If the program has a systray icon, it will have a hidden window as well, and you can try sending WM_QUERYENDSESSION and WM_ENDSESSION to it. However, there is no guarentee that the program will shut down, and if it does shut down, since you may be bypassing parts of its normal shutdown process, it may not complete its normal cleanup. There may also be programs running with no associated windows at all. The best approach would probably be to simply log off the user, and perform the backup from a service process. Of course, you'll still have sharing issues with files opened by other services...

like image 30
bdonlan Avatar answered Oct 08 '22 22:10

bdonlan