7

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

user568160
  • 91
  • 2
  • 3
    `TerminateProcess` does not leave files open. All open handles are closed when the owning process closes. Besides, if you want to back up open files, the OS already provides an API for that: Shadow Copy. – Rob Kennedy Jun 17 '11 at 22:38
  • @Rob: Trying to back-up open files could cause you to take a snapshot while the file is in an inconsistent state. – Gabe Jun 17 '11 at 22:50
  • 1
    @Gabe, no worse than shutting down programs unexpectedly... – bdonlan Jun 17 '11 at 23:20
  • 1
    @bdonlan: Yes, that's the point of the question. – Gabe Jun 17 '11 at 23:56

2 Answers2

8

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    +1. You have no idea what will trigger the program to do its cleanup operations. Sure, you can send its hidden window a WM_QUIT or something, but that might well skip all cleanup just like with TerminateProcess – bdonlan Jun 17 '11 at 23:21
  • I realize that, but at least using wm_Close is better than just slamming it shut with TerminateProcess. A gentle Close is more likely to cover all of the clean up needed. If not, at least my app tried to close things gently. See my other comment below. – user568160 Jun 18 '11 at 17:03
2

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...

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • Thanks guys, but it was Shadow Copy that first alerted me to the problem. With Shadow Copy any programs that decrypt files for exclusive use while running and then encrypt on Close are copied in the "Clear" state. All of my Passwords were copied and backed up in the Clear!! {gasp} That's why I want to close kindly and let apps go through their Close process which Terminate does not do. – user568160 Jun 18 '11 at 17:00
  • 2
    Those programs are broken then; that's all there is to it. You could try skipping temporary directories, but if a password program is decrypting its password database and leaving it on disk, it's already making the encryption useless (an attacker could just scan the disk for deleted files!) – bdonlan Jun 18 '11 at 20:10